module.exports = (sequelize, DataTypes) => {
  const CorrectiveActionReportBatch = sequelize.define('CorrectiveActionReportBatch', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    report_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'CorrectiveActionReport', key: 'id' },
      onDelete: 'CASCADE'
    },
    line_no: { type: DataTypes.INTEGER, allowNull: false },
    next_batch_date: { type: DataTypes.DATEONLY, allowNull: true },
    batch: { type: DataTypes.STRING(100), allowNull: true },
    qty: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    accepted: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    nc_qty: { type: DataTypes.DECIMAL(15, 3), allowNull: true }
  }, {
    tableName: 'corrective_action_report_batch',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [{ fields: ['report_id'] }]
  });

  CorrectiveActionReportBatch.associate = function(models) {
    CorrectiveActionReportBatch.belongsTo(models.CorrectiveActionReport, { foreignKey: 'report_id', as: 'report' });
  };

  return CorrectiveActionReportBatch;
};
