module.exports = (sequelize, DataTypes) => {
  const ProductionPacking = sequelize.define(
    'ProductionPacking',
    {
      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' }
      },
      packing_number: {
        type: DataTypes.STRING(60),
        allowNull: false,
        unique: true
      },
      route_card_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'RouteCard', key: 'id' }
      },
      reference_packing_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'ProductionPacking', key: 'id' }
      },
      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' }
      },
      finished_material_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'Material', key: 'id' }
      },
      accepted_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      packed_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      balance_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      packer_ids: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      related_route_card_ids: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      actual_start: {
        type: DataTypes.DATE,
        allowNull: true
      },
      actual_end: {
        type: DataTypes.DATE,
        allowNull: true
      },
      remarks: {
        type: DataTypes.TEXT,
        allowNull: true
      },
      status: {
        type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'PARTIAL_COMPLETED', 'COMPLETED'),
        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_packing',
      indexes: [
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { unique: true, fields: ['packing_number'] },
        { fields: ['route_card_id'] },
        { fields: ['reference_packing_id'] },
        { fields: ['status'] }
      ]
    }
  );

  ProductionPacking.associate = function (models) {
    ProductionPacking.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    ProductionPacking.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    ProductionPacking.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    ProductionPacking.belongsTo(models.RouteCard, { foreignKey: 'route_card_id', as: 'routeCard' });
    ProductionPacking.belongsTo(models.ProductionPacking, { foreignKey: 'reference_packing_id', as: 'referencePacking' });
    ProductionPacking.hasMany(models.ProductionPacking, { foreignKey: 'reference_packing_id', as: 'childPackings' });
    ProductionPacking.belongsTo(models.WorkOrder, { foreignKey: 'work_order_id', as: 'workOrder' });
    ProductionPacking.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    ProductionPacking.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    ProductionPacking.belongsTo(models.Material, { foreignKey: 'finished_material_id', as: 'finishedMaterial' });
    ProductionPacking.hasMany(models.ProductionFGInward, { foreignKey: 'packing_id', as: 'fgInwardEntries' });
    ProductionPacking.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    ProductionPacking.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return ProductionPacking;
};

