module.exports = (sequelize, DataTypes) => {
  const RejectionReworkRecord = sequelize.define('RejectionReworkRecord', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    company_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'Company', key: 'id' } },
    report_no: { type: DataTypes.STRING(50), allowNull: true },
    record_date: { 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: 'rejection_rework_record',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['report_no'] },
      { fields: ['record_date'] }
    ]
  });

  RejectionReworkRecord.associate = function(models) {
    RejectionReworkRecord.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    RejectionReworkRecord.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    RejectionReworkRecord.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
    RejectionReworkRecord.hasMany(models.RejectionReworkRecordDetail, { foreignKey: 'record_id', as: 'details' });
  };

  return RejectionReworkRecord;
};
