'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('approval_master', {
      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'
      },
      module_name: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: 'Module name: PURCHASE_ORDER, SALES_ORDER, CUSTOMER_PO, etc.'
      },
      transaction_type: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: 'Specific transaction type within module'
      },
      approval_level: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: 'Approval level (1, 2, 3, etc.)'
      },
      approver_role_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'role',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Role required for approval at this level'
      },
      approver_user_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: 'Specific user required for approval (if not role-based)'
      },
      min_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: true,
        defaultValue: 0,
        comment: 'Minimum amount threshold for this approval level'
      },
      max_amount: {
        type: Sequelize.DECIMAL(18, 2),
        allowNull: true,
        comment: 'Maximum amount threshold for this approval level (null = no limit)'
      },
      is_required: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: 'Whether this approval level is required'
      },
      sequence: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: 'Sequence order for approval levels'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      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_master', {
      fields: ['company_id', 'module_name', 'transaction_type', 'approval_level', 'sequence'],
      unique: true,
      name: 'unique_approval_master'
    });

    await queryInterface.addIndex('approval_master', {
      fields: ['module_name', 'status'],
      name: 'idx_approval_master_module_status'
    });
  },

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

