'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('approval_transaction', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      approval_master_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'approval_master',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      module_name: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Module name: PURCHASE_ORDER, SALES_ORDER, etc.'
      },
      transaction_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'ID of the transaction being approved (PO ID, SO ID, etc.)'
      },
      transaction_number: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Transaction number for reference'
      },
      approval_level: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: 'Current approval level'
      },
      status: {
        type: Sequelize.ENUM('PENDING', 'APPROVED', 'REJECTED', 'CANCELLED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      approver_role_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'role',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      approver_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'User who approved/rejected'
      },
      approved_at: {
        type: Sequelize.DATE,
        allowNull: true
      },
      rejection_reason: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      comments: {
        type: Sequelize.TEXT,
        allowNull: true
      },
      transaction_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: true,
        comment: 'Amount of the transaction for reference'
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('approval_transaction', {
      fields: ['module_name', 'transaction_id', 'status'],
      name: 'idx_approval_transaction_module_txn_status'
    });

    await queryInterface.addIndex('approval_transaction', {
      fields: ['approval_master_id'],
      name: 'idx_approval_transaction_master'
    });

    await queryInterface.addIndex('approval_transaction', {
      fields: ['approver_user_id'],
      name: 'idx_approval_transaction_approver'
    });
  },

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

