module.exports = (sequelize, DataTypes) => {
  const ProductionQC = sequelize.define(
    'ProductionQC',
    {
      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' }
      },
      route_card_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'RouteCard', key: 'id' }
      },
      route_card_operation_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        unique: true,
        references: { model: 'RouteCardOperation', key: 'id' }
      },
      production_qc_number: {
        type: DataTypes.STRING(50),
        allowNull: true,
        unique: true
      },
      work_order_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'WorkOrder', key: 'id' }
      },
      production_order_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'ProductionOrder', key: 'id' }
      },
      operation_sequence_no: {
        type: DataTypes.INTEGER,
        allowNull: false
      },
      operation_code: {
        type: DataTypes.STRING(100),
        allowNull: true
      },
      operation_name: {
        type: DataTypes.STRING(500),
        allowNull: true
      },
      total_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      accepted_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      checked_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rework_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rework_remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      fr_rejected_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      mr_rejected_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejected_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      rejection_remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      status: {
        type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'APPROVED', 'FAILED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      moved_to_qc_at: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW
      },
      moved_to_qc_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      qc_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      qc_by_ids: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      qc_at: {
        type: DataTypes.DATE,
        allowNull: true
      },
      actual_start: {
        type: DataTypes.DATE,
        allowNull: true
      },
      actual_end: {
        type: DataTypes.DATE,
        allowNull: true
      }
    },
    {
      tableName: 'production_qc',
      indexes: [
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { fields: ['route_card_id'] },
        { unique: true, fields: ['route_card_operation_id'] },
        { fields: ['status'] }
      ]
    }
  );

  ProductionQC.associate = function (models) {
    ProductionQC.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionQC.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionQC.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ProductionQC.belongsTo(models.RouteCard, { foreignKey: 'route_card_id', as: 'routeCard' });
    ProductionQC.belongsTo(models.RouteCardOperation, { foreignKey: 'route_card_operation_id', as: 'routeCardOperation' });
    ProductionQC.belongsTo(models.WorkOrder, { foreignKey: 'work_order_id', as: 'workOrder' });
    ProductionQC.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionQC.belongsTo(models.User, { foreignKey: 'moved_to_qc_by', as: 'movedByUser' });
    ProductionQC.belongsTo(models.User, { foreignKey: 'qc_by', as: 'qcByUser' });
    ProductionQC.hasOne(models.RouteCard, { foreignKey: 'source_production_qc_id', as: 'reworkRouteCard' });
    ProductionQC.hasMany(models.ProductionQCComponentTracking, { foreignKey: 'production_qc_id', as: 'componentTrackings' });
  };

  return ProductionQC;
};
