module.exports = (sequelize, DataTypes) => {
  const MusterRoll = sequelize.define('MusterRoll', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    muster_roll_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'
      }
    },
    month: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        min: 1,
        max: 12
      }
    },
    year: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    total_days: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    present_days: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    absent_days: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    leave_days: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    holiday_days: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    week_off_days: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    overtime_hours: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      defaultValue: 0
    },
    total_work_hours: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: true,
      defaultValue: 0
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'LOCKED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    }
  }, {
    tableName: 'muster_roll',
    indexes: [
      {
        unique: true,
        fields: ['muster_roll_number']
      },
      {
        fields: ['company_id']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['employee_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        unique: true,
        fields: ['employee_id', 'month', 'year', 'fiscal_year_id']
      }
    ]
  });

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

  return MusterRoll;
};

