"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    // ─── 1. KybApplications ───
    await queryInterface.createTable("KybApplications", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      merchantId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "Merchants", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      status: {
        type: Sequelize.ENUM(
          "Draft",
          "Submitted",
          "Under Review",
          "Additional Documents Required",
          "Approved",
          "Rejected"
        ),
        defaultValue: "Draft",
        allowNull: false,
      },
      companyName: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      registrationNumber: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      companyAddress: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      companyType: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      businessDescription: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      website: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      incorporationDate: {
        type: Sequelize.DATEONLY,
        allowNull: true,
      },
      taxId: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      verificationOutcome: {
        type: Sequelize.JSON,
        allowNull: true,
      },
      rejectionReason: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      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"),
      },
    });

    // ─── 2. KybDocuments ───
    await queryInterface.createTable("KybDocuments", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      applicationId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "KybApplications", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      documentType: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      documentUrl: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      status: {
        type: Sequelize.ENUM("Pending", "Approved", "Rejected", "ActionRequired"),
        defaultValue: "Pending",
      },
      rejectionReason: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      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"),
      },
    });

    // ─── 3. KybDirectors ───
    await queryInterface.createTable("KybDirectors", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      applicationId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "KybApplications", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      firstName: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      lastName: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      email: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      phoneNumber: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      role: {
        type: Sequelize.ENUM("Director", "Beneficial Owner", "Other"),
        defaultValue: "Director",
        allowNull: false,
      },
      ownershipPercentage: {
        type: Sequelize.DECIMAL(5, 2),
        allowNull: true,
      },
      idType: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      idNumber: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      idDocumentUrl: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      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"),
      },
    });

    // ─── 4. KybHistories ───
    await queryInterface.createTable("KybHistories", {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
      },
      applicationId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "KybApplications", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "CASCADE",
      },
      adminId: {
        type: Sequelize.UUID,
        allowNull: true,
        references: { model: "Admins", key: "id" },
        onUpdate: "CASCADE",
        onDelete: "SET NULL",
      },
      action: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      previousStatus: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      newStatus: {
        type: Sequelize.STRING,
        allowNull: true,
      },
      comment: {
        type: Sequelize.TEXT,
        allowNull: true,
      },
      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"),
      },
    });

    // ─── Indexes ───
    await queryInterface.addIndex("KybApplications", ["merchantId", "status"]);
    await queryInterface.addIndex("KybDocuments", ["applicationId"]);
    await queryInterface.addIndex("KybDirectors", ["applicationId"]);
    await queryInterface.addIndex("KybHistories", ["applicationId"]);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable("KybHistories");
    await queryInterface.dropTable("KybDirectors");
    await queryInterface.dropTable("KybDocuments");
    await queryInterface.dropTable("KybApplications");
  },
};
