"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("Otps", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      userId: {
        type: Sequelize.UUID,
        allowNull: false,
      },
      userType: {
        type: Sequelize.ENUM("admin", "merchant"),
        allowNull: false,
      },
      otp: {
        type: Sequelize.STRING,
        allowNull: false,
        comment: "Hashed OTP code",
      },
      purpose: {
        type: Sequelize.ENUM("login", "payout", "password_reset"),
        allowNull: false,
      },
      expiresAt: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      isUsed: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
      },
      attempts: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
      },
    });

    await queryInterface.addIndex("Otps", ["userId", "purpose", "isUsed"]);
  },

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