'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('ExchangeRates', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
        allowNull: false
      },
      baseCurrency: {
        type: Sequelize.STRING(10),
        allowNull: false
      },
      targetCurrency: {
        type: Sequelize.STRING(10),
        allowNull: false
      },
      rate: {
        type: Sequelize.DECIMAL(15, 4),
        defaultValue: 0.0000
      },
      rateSource: {
        type: Sequelize.STRING(20),
        defaultValue: 'manual'
      },
      margin: {
        type: Sequelize.DECIMAL(15, 4),
        allowNull: false
      },
      marginOperation: {
        type: Sequelize.ENUM('add', 'subtract'),
        allowNull: false
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });

    await queryInterface.addIndex('ExchangeRates', ['baseCurrency', 'targetCurrency'], {
      name: 'exchange_rates_currencies_idx'
    });
  },

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