"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("Users", {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      email: {
        type: Sequelize.STRING,
        unique: true,
        allowNull: false,
      },
      password: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      firstName: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      lastName: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      phone: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      authProvider: {
        type: Sequelize.ENUM("local", "google"),
        defaultValue: "local",
      },
      providerId: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      address1: {
        type: Sequelize.STRING,
      },
      address2: {
        type: Sequelize.STRING,
      },
      city: {
        type: Sequelize.STRING,
      },
      state: {
        type: Sequelize.STRING,
      },
      zipCode: {
        type: Sequelize.STRING,
      },
      country: {
        type: Sequelize.STRING,
      },
      profileImage: {
        type: Sequelize.STRING,
      },
      isVerified: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      verificationToken: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      verificationTokenExpiry: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      verifiedAt: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      passwordResetToken: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      passwordResetTokenExpiry: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      defaultAvailableAfter: {
        type: Sequelize.STRING,
        defaultValue: "09:00",
      },
      defaultAvailableBefore: {
        type: Sequelize.STRING,
        defaultValue: "17:00",
      },
      defaultSpecifyTimesPerDay: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      defaultWeeklyTimes: {
        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" },
        },
      },
      stripeConnectedAccountId: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      stripeCustomerId: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      loginAttempts: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
      },
      lockUntil: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      jwtVersion: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        allowNull: false,
      },
      role: {
        type: Sequelize.ENUM("user", "admin"),
        defaultValue: "user",
        allowNull: false,
      },
      itemRequestNotificationRadius: {
        type: Sequelize.INTEGER,
        defaultValue: 10,
        allowNull: true,
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // Add indexes
    await queryInterface.addIndex("Users", ["email"], { unique: true });
  },

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