'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Transactions', {
      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'
      },
      customerId: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'Customers',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      transaction_id: {
        type: Sequelize.STRING,
        allowNull: false
      },
      type: {
        type: Sequelize.ENUM('debit', 'credit'),
        allowNull: false
      },
      class: {
        type: Sequelize.ENUM('payout', 'deposit', 'transfer'),
        allowNull: false
      },
      amount: {
        type: Sequelize.DECIMAL(20, 2),
        allowNull: false
      },
      fee: {
        type: Sequelize.DECIMAL(20, 2),
        defaultValue: 0.00
      },
      total_amount: {
        type: Sequelize.DECIMAL(20, 2),
        allowNull: false
      },
      status: {
        type: Sequelize.ENUM('pending', 'success', 'failed'),
        defaultValue: 'pending'
      },
      info: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      currency: {
        type: Sequelize.STRING,
        defaultValue: 'NGN'
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    // --- Performance Indexes ---
    await queryInterface.addIndex('Transactions', ['transaction_id']);
    await queryInterface.addIndex('Transactions', ['merchantId']);
    await queryInterface.addIndex('Transactions', ['customerId']);
    await queryInterface.addIndex('Transactions', ['createdAt']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('Transactions');
  }
};
