module.exports = (sequelize, DataTypes) => {
  const WeldingFinalInspectionReport = sequelize.define('WeldingFinalInspectionReport', {
    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'
      }
    },
    wfir_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      comment: 'Welding Final Inspection Report Number'
    },
    wfir_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    doc_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Document Number - RI/QA/F/04'
    },
    rev_no: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: 'Revision Number'
    },
    rev_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Revision Date'
    },
    model: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Model/Product Name'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    customer_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    fabricator_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: 'Fabricator Name'
    },
    won: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Work Order Number'
    },
    total_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0,
      comment: 'Total Quantity'
    },
    sample_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0,
      comment: 'Sample Quantity'
    },
    uom: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: 'Unit of Measurement'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    fabricator_sign: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Fabricator Signature'
    },
    fabricator_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    qc_sign: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'QC Signature'
    },
    qc_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    inspection_status: {
      type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'APPROVED', 'REJECTED'),
      allowNull: false,
      defaultValue: 'PENDING'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    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: 'welding_final_inspection_report',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['plant_id'] },
      { unique: true, fields: ['wfir_no'] },
      { fields: ['wfir_date'] },
      { fields: ['customer_id'] },
      { fields: ['status'] },
      { fields: ['inspection_status'] }
    ]
  });

  WeldingFinalInspectionReport.associate = function(models) {
    WeldingFinalInspectionReport.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    WeldingFinalInspectionReport.belongsTo(models.Plant, {
      foreignKey: 'plant_id',
      as: 'plant'
    });

    WeldingFinalInspectionReport.belongsTo(models.FiscalYear, {
      foreignKey: 'fiscal_year_id',
      as: 'fiscalYear'
    });

    WeldingFinalInspectionReport.belongsTo(models.BusinessPartner, {
      foreignKey: 'customer_id',
      as: 'customer'
    });

    WeldingFinalInspectionReport.belongsTo(models.User, {
      foreignKey: 'fabricator_user_id',
      as: 'fabricator'
    });

    WeldingFinalInspectionReport.belongsTo(models.User, {
      foreignKey: 'qc_user_id',
      as: 'qcInspector'
    });

    WeldingFinalInspectionReport.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    WeldingFinalInspectionReport.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });

    WeldingFinalInspectionReport.hasMany(models.WeldingFinalInspectionReportDetail, {
      foreignKey: 'wfir_id',
      as: 'details'
    });
  };

  return WeldingFinalInspectionReport;
};
