module.exports = (sequelize, DataTypes) => {
  const Payroll = sequelize.define('Payroll', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    payroll_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    employee_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Employee',
        key: 'id'
      }
    },
    muster_roll_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MusterRoll',
        key: 'id'
      }
    },
    month: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        min: 1,
        max: 12
      }
    },
    year: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    payroll_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    basic_salary: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_earnings: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_deductions: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0
    },
    gross_salary: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0
    },
    net_salary: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'CALCULATED', 'APPROVED', 'POSTED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    }
  }, {
    tableName: 'payroll',
    indexes: [
      {
        unique: true,
        fields: ['payroll_number']
      },
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['employee_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['muster_roll_id']
      },
      {
        unique: true,
        fields: ['employee_id', 'month', 'year', 'fiscal_year_id']
      }
    ]
  });

  Payroll.associate = function(models) {
    Payroll.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Payroll.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    Payroll.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    Payroll.belongsTo(models.Employee, { foreignKey: 'employee_id', as: 'employee' });
    Payroll.belongsTo(models.MusterRoll, { foreignKey: 'muster_roll_id', as: 'musterRoll' });
    Payroll.hasMany(models.PayrollComponent, { foreignKey: 'payroll_id', as: 'components' });
    Payroll.hasMany(models.Payslip, { foreignKey: 'payroll_id', as: 'payslips' });
  };

  return Payroll;
};

