"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("Items", {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      pickUpAvailable: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      localDeliveryAvailable: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      localDeliveryRadius: {
        type: Sequelize.INTEGER,
      },
      shippingAvailable: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      inPlaceUseAvailable: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },
      pricePerHour: {
        type: Sequelize.DECIMAL(10, 2),
      },
      pricePerDay: {
        type: Sequelize.DECIMAL(10, 2),
      },
      pricePerWeek: {
        type: Sequelize.DECIMAL(10, 2),
      },
      pricePerMonth: {
        type: Sequelize.DECIMAL(10, 2),
      },
      replacementCost: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false,
      },
      address1: {
        type: Sequelize.STRING,
      },
      address2: {
        type: Sequelize.STRING,
      },
      city: {
        type: Sequelize.STRING,
      },
      state: {
        type: Sequelize.STRING,
      },
      zipCode: {
        type: Sequelize.STRING,
      },
      country: {
        type: Sequelize.STRING,
      },
      latitude: {
        type: Sequelize.DECIMAL(10, 8),
      },
      longitude: {
        type: Sequelize.DECIMAL(11, 8),
      },
      images: {
        type: Sequelize.ARRAY(Sequelize.STRING),
        defaultValue: [],
      },
      isAvailable: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
      },
      rules: {
        type: Sequelize.TEXT,
      },
      availableAfter: {
        type: Sequelize.STRING,
        defaultValue: "09:00",
      },
      availableBefore: {
        type: Sequelize.STRING,
        defaultValue: "17:00",
      },
      specifyTimesPerDay: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      weeklyTimes: {
        type: Sequelize.JSONB,
        defaultValue: {
          sunday: { availableAfter: "09:00", availableBefore: "17:00" },
          monday: { availableAfter: "09:00", availableBefore: "17:00" },
          tuesday: { availableAfter: "09:00", availableBefore: "17:00" },
          wednesday: { availableAfter: "09:00", availableBefore: "17:00" },
          thursday: { availableAfter: "09:00", availableBefore: "17:00" },
          friday: { availableAfter: "09:00", availableBefore: "17:00" },
          saturday: { availableAfter: "09:00", availableBefore: "17:00" },
        },
      },
      ownerId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      isDeleted: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      deletedBy: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: "Users",
          key: "id",
        },
        onUpdate: "CASCADE",
        onDelete: "SET NULL",
      },
      deletedAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      deletionReason: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // Add indexes
    await queryInterface.addIndex("Items", ["ownerId"]);
    await queryInterface.addIndex("Items", ["isAvailable"]);
    await queryInterface.addIndex("Items", ["isDeleted"]);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable("Items");
  },
};
