module.exports = (sequelize, DataTypes) => {
  const ProductionFinalInspection = sequelize.define(
    'ProductionFinalInspection',
    {
      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,
        unique: true,
        references: { model: 'RouteCard', key: 'id' }
      },
      final_inspection_number: {
        type: DataTypes.STRING(60),
        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' }
      },
      customer_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'BusinessPartner', key: 'id' }
      },
      order_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_from_operation_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'RouteCardOperation', key: 'id' }
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      qc_by: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'User', key: 'id' }
      },
      qc_by_ids: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      started_at: {
        type: DataTypes.DATE,
        allowNull: true
      },
      completed_at: {
        type: DataTypes.DATE,
        allowNull: true
      },
      status: {
        type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'APPROVED'),
        allowNull: false,
        defaultValue: 'PENDING'
      },
      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: 'production_final_inspection',
      indexes: [
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { unique: true, fields: ['route_card_id'] },
        { unique: true, fields: ['final_inspection_number'] },
        { fields: ['status'] }
      ]
    }
  );

  ProductionFinalInspection.associate = function (models) {
    ProductionFinalInspection.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionFinalInspection.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionFinalInspection.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ProductionFinalInspection.belongsTo(models.RouteCard, { foreignKey: 'route_card_id', as: 'routeCard' });
    ProductionFinalInspection.belongsTo(models.WorkOrder, { foreignKey: 'work_order_id', as: 'workOrder' });
    ProductionFinalInspection.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionFinalInspection.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    ProductionFinalInspection.belongsTo(models.User, { foreignKey: 'qc_by', as: 'qcByUser' });
  };

  return ProductionFinalInspection;
};
