module.exports = (sequelize, DataTypes) => {
  const CorrectiveActionReport = sequelize.define('CorrectiveActionReport', {
    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 },
    report_date: { type: DataTypes.DATEONLY, allowNull: true },
    customer_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'BusinessPartner', key: 'id' } },
    customer_name: { type: DataTypes.STRING(200), allowNull: true },
    nc_ref: { type: DataTypes.STRING(100), allowNull: true },
    customer_supplier: { type: DataTypes.STRING(300), allowNull: true },
    problem_located_at: { type: DataTypes.STRING(100), allowNull: true },
    nature_of_problem: { type: DataTypes.STRING(50), allowNull: true },
    part_name: { type: DataTypes.STRING(200), allowNull: true },
    part_number: { type: DataTypes.STRING(100), allowNull: true },
    quantity: { type: DataTypes.DECIMAL(15, 3), allowNull: true },
    drawing_no: { type: DataTypes.STRING(100), allowNull: true },
    sub_contract_dc_no_date: { type: DataTypes.STRING(200), allowNull: true },
    melt_no_heat_no: { type: DataTypes.STRING(100), allowNull: true },
    prepared_by: { type: DataTypes.STRING(200), allowNull: true },
    part_reinspected_by_date: { type: DataTypes.STRING(200), allowNull: true },
    communicated_to: { type: DataTypes.STRING(200), allowNull: true },
    approved_by: { type: DataTypes.STRING(200), allowNull: true },
    disposition: { type: DataTypes.STRING(50), allowNull: true },
    root_cause: { type: DataTypes.TEXT, allowNull: true },
    analyzed_by: { type: DataTypes.STRING(200), allowNull: true },
    interim_corrective_action: { type: DataTypes.TEXT, allowNull: true },
    corrective_action_permanent: { type: DataTypes.TEXT, allowNull: true },
    verified_by: { type: DataTypes.STRING(200), allowNull: true },
    standardization: { type: DataTypes.TEXT, allowNull: true },
    corrective_action_impact: { type: DataTypes.TEXT, 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: 'corrective_action_report',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['report_no'] },
      { fields: ['report_date'] }
    ]
  });

  CorrectiveActionReport.associate = function(models) {
    CorrectiveActionReport.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    CorrectiveActionReport.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    CorrectiveActionReport.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    CorrectiveActionReport.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
    CorrectiveActionReport.hasMany(models.CorrectiveActionReportInterim, { foreignKey: 'report_id', as: 'interimActions' });
    CorrectiveActionReport.hasMany(models.CorrectiveActionReportPermanent, { foreignKey: 'report_id', as: 'permanentActions' });
    CorrectiveActionReport.hasMany(models.CorrectiveActionReportBatch, { foreignKey: 'report_id', as: 'batchDetails' });
  };

  return CorrectiveActionReport;
};
