module.exports = (sequelize, DataTypes) => {
  const RejectionReworkRecordDetail = sequelize.define('RejectionReworkRecordDetail', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    record_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'RejectionReworkRecord', key: 'id' },
      onDelete: 'CASCADE'
    },
    line_no: { type: DataTypes.INTEGER, allowNull: false },
    date: { type: DataTypes.DATEONLY, allowNull: true },
    machine_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'Machine', key: 'id' } },
    machine_name: { type: DataTypes.STRING(200), allowNull: true },
    operator: { type: DataTypes.STRING(200), allowNull: true },
    part_description: { type: DataTypes.STRING(500), allowNull: true },
    drg_no: { type: DataTypes.STRING(100), allowNull: true },
    qty_prod: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    qty_rej: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    qty_rew: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    reason_for_rej: { type: DataTypes.TEXT, allowNull: true },
    disposal_action: { type: DataTypes.STRING(200), allowNull: true },
    signature: { type: DataTypes.STRING(200), allowNull: true }
  }, {
    tableName: 'rejection_rework_record_detail',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [{ fields: ['record_id'] }]
  });

  RejectionReworkRecordDetail.associate = function(models) {
    RejectionReworkRecordDetail.belongsTo(models.RejectionReworkRecord, { foreignKey: 'record_id', as: 'record' });
    RejectionReworkRecordDetail.belongsTo(models.Machine, { foreignKey: 'machine_id', as: 'machine' });
  };

  return RejectionReworkRecordDetail;
};
