const { DataTypes } = require("sequelize");
const sequelize = require("../config/database");

const Item = sequelize.define("Item", {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
  pickUpAvailable: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  localDeliveryAvailable: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  localDeliveryRadius: {
    type: DataTypes.INTEGER,
    validate: {
      min: 1,
      max: 100,
    },
  },
  shippingAvailable: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  inPlaceUseAvailable: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  pricePerHour: {
    type: DataTypes.DECIMAL(10, 2),
  },
  pricePerDay: {
    type: DataTypes.DECIMAL(10, 2),
  },
  pricePerWeek: {
    type: DataTypes.DECIMAL(10, 2),
  },
  pricePerMonth: {
    type: DataTypes.DECIMAL(10, 2),
  },
  replacementCost: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
  },
  address1: {
    type: DataTypes.STRING,
  },
  address2: {
    type: DataTypes.STRING,
  },
  city: {
    type: DataTypes.STRING,
  },
  state: {
    type: DataTypes.STRING,
  },
  zipCode: {
    type: DataTypes.STRING,
  },
  country: {
    type: DataTypes.STRING,
  },
  latitude: {
    type: DataTypes.DECIMAL(10, 8),
  },
  longitude: {
    type: DataTypes.DECIMAL(11, 8),
  },
  imageFilenames: {
    type: DataTypes.ARRAY(DataTypes.TEXT),
    defaultValue: [],
  },
  isAvailable: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
  },
  rules: {
    type: DataTypes.TEXT,
  },
  availableAfter: {
    type: DataTypes.STRING,
    defaultValue: "00:00",
  },
  availableBefore: {
    type: DataTypes.STRING,
    defaultValue: "23:00",
  },
  specifyTimesPerDay: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
  },
  weeklyTimes: {
    type: DataTypes.JSONB,
    defaultValue: {
      sunday: { availableAfter: "00:00", availableBefore: "23:00" },
      monday: { availableAfter: "00:00", availableBefore: "23:00" },
      tuesday: { availableAfter: "00:00", availableBefore: "23:00" },
      wednesday: { availableAfter: "00:00", availableBefore: "23:00" },
      thursday: { availableAfter: "00:00", availableBefore: "23:00" },
      friday: { availableAfter: "00:00", availableBefore: "23:00" },
      saturday: { availableAfter: "00:00", availableBefore: "23:00" },
    },
  },
  ownerId: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: "Users",
      key: "id",
    },
  },
  isDeleted: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
  },
  deletedBy: {
    type: DataTypes.UUID,
    allowNull: true,
    references: {
      model: "Users",
      key: "id",
    },
  },
  deletedAt: {
    type: DataTypes.DATE,
    allowNull: true,
  },
  deletionReason: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
});

module.exports = Item;
