module.exports = (sequelize, DataTypes) => {
  const PayrollComponent = sequelize.define('PayrollComponent', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    payroll_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Payroll',
        key: 'id',
        onDelete: 'CASCADE'
      }
    },
    component_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    component_code: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    component_type: {
      type: DataTypes.ENUM('EARNING', 'DEDUCTION'),
      allowNull: false
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0
    },
    calculation_type: {
      type: DataTypes.ENUM('FIXED', 'PERCENTAGE', 'FORMULA'),
      allowNull: false,
      defaultValue: 'FIXED'
    },
    calculation_value: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true
    },
    display_order: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    is_statutory: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'payroll_component',
    indexes: [
      {
        fields: ['payroll_id']
      },
      {
        fields: ['component_type']
      },
      {
        fields: ['component_code']
      }
    ]
  });

  PayrollComponent.associate = function(models) {
    PayrollComponent.belongsTo(models.Payroll, { foreignKey: 'payroll_id', as: 'payroll' });
  };

  return PayrollComponent;
};

