module.exports = (sequelize, DataTypes) => {
  const ApprovalMaster = sequelize.define('ApprovalMaster', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    module_name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Module name: PURCHASE_ORDER, SALES_ORDER, CUSTOMER_PO, etc.'
    },
    transaction_type: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Specific transaction type within module'
    },
    approval_level: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: 'Approval level (1, 2, 3, etc.)'
    },
    approver_role_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Role',
        key: 'id'
      },
      comment: 'Role required for approval at this level'
    },
    approver_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'Specific user required for approval (if not role-based)'
    },
    min_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: true,
      defaultValue: 0,
      comment: 'Minimum amount threshold for this approval level'
    },
    max_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: true,
      comment: 'Maximum amount threshold for this approval level (null = no limit)'
    },
    is_required: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: 'Whether this approval level is required'
    },
    sequence: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: 'Sequence order for approval levels'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'approval_master',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'module_name', 'transaction_type', 'approval_level', 'sequence']
      },
      {
        fields: ['module_name', 'status']
      },
      {
        fields: ['approver_role_id']
      },
      {
        fields: ['approver_user_id']
      }
    ]
  });

  ApprovalMaster.associate = function(models) {
    ApprovalMaster.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ApprovalMaster.belongsTo(models.Role, { foreignKey: 'approver_role_id', as: 'approverRole' });
    ApprovalMaster.belongsTo(models.User, { foreignKey: 'approver_user_id', as: 'approverUser' });
    ApprovalMaster.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    ApprovalMaster.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
    ApprovalMaster.hasMany(models.ApprovalTransaction, { foreignKey: 'approval_master_id', as: 'approvalTransactions' });
  };

  return ApprovalMaster;
};

