"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable("MerchantServicePricings", {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
        allowNull: false,
      },
      merchantId: {
        type: Sequelize.UUID,
        allowNull: false,
        references: { model: "Merchants", key: "id" },
        onDelete: "CASCADE",
      },
      serviceName: {
        type: Sequelize.STRING(50),
        allowNull: false,
      },
      currency: {
        type: Sequelize.STRING(10),
        allowNull: false,
      },
      providerPercentage: {
        type: Sequelize.DECIMAL(5, 4),
        defaultValue: 0.0000,
      },
      providerFixedFee: {
        type: Sequelize.DECIMAL(15, 2),
        defaultValue: 0.00,
      },
      providerMinCap: {
        type: Sequelize.DECIMAL(15, 2),
        defaultValue: 0.00,
      },
      providerMaxCap: {
        type: Sequelize.DECIMAL(15, 2),
        defaultValue: 999999999.99,
      },
      customerPercentage: {
        type: Sequelize.DECIMAL(5, 4),
        defaultValue: 0.0000,
      },
      customerFixedFee: {
        type: Sequelize.DECIMAL(15, 2),
        defaultValue: 0.00,
      },
      customerMinCap: {
        type: Sequelize.DECIMAL(15, 2),
        defaultValue: 0.00,
      },
      customerMaxCap: {
        type: Sequelize.DECIMAL(15, 2),
        defaultValue: 999999999.99,
      },
      isActive: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
      },
      createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal("NOW()"),
      },
      updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal("NOW()"),
      },
    });

    // One override per merchant per service per currency
    await queryInterface.addIndex("MerchantServicePricings", ["merchantId", "serviceName", "currency"], {
      unique: true,
      name: "unique_merchant_service_pricing",
    });
  },

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