module.exports = (sequelize, DataTypes) => {
  const AssemblyFinalInspectionReport = sequelize.define('AssemblyFinalInspectionReport', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Company', key: 'id' }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'Plant', key: 'id' }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'FiscalYear', key: 'id' }
    },
    report_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'Report Number (e.g. AFIR-2026-001)'
    },
    report_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      comment: 'Report Date'
    },
    doc_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      defaultValue: 'RI/QA/F/05',
      comment: 'Document Number'
    },
    rev_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      defaultValue: '0 / 05-10-2018',
      comment: 'Revision No / Date'
    },
    model: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Model'
    },
    work_order_no: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Work Order Number (WON)'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'BusinessPartner', key: 'id' }
    },
    customer_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Customer name (cached)'
    },
    total_qty: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Total Quantity'
    },
    sample_qty: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      comment: 'Sample Quantity'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: 'Remarks'
    },
    charge_sign_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' },
      comment: 'In-charge sign user ID'
    },
    inspected_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'User', key: 'id' },
      comment: 'Inspected by user ID'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT',
      comment: 'Document status'
    },
    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: 'assembly_final_inspection_report',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['plant_id'] },
      { unique: true, fields: ['report_no'] },
      { fields: ['report_date'] },
      { fields: ['customer_id'] },
      { fields: ['status'] }
    ]
  });

  AssemblyFinalInspectionReport.associate = function(models) {
    AssemblyFinalInspectionReport.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    AssemblyFinalInspectionReport.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    AssemblyFinalInspectionReport.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    AssemblyFinalInspectionReport.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    AssemblyFinalInspectionReport.belongsTo(models.User, { foreignKey: 'charge_sign_by', as: 'chargeSignBy' });
    AssemblyFinalInspectionReport.belongsTo(models.User, { foreignKey: 'inspected_by', as: 'inspectedByUser' });
    AssemblyFinalInspectionReport.belongsTo(models.User, { foreignKey: 'created_by', as: 'creator' });
    AssemblyFinalInspectionReport.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updater' });
    AssemblyFinalInspectionReport.hasMany(models.AssemblyFinalInspectionReportDetail, { foreignKey: 'report_id', as: 'details' });
  };

  return AssemblyFinalInspectionReport;
};
