module.exports = (sequelize, DataTypes) => {
  const SerialNumberTracking = sequelize.define('SerialNumberTracking', {
    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'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    serial_number: {
      type: DataTypes.STRING(100),
      allowNull: false,
      comment: 'Unique serial number'
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Batch number if material is batch managed'
    },
    current_status: {
      type: DataTypes.ENUM('IN_STOCK', 'ISSUED', 'IN_PRODUCTION', 'DELIVERED', 'SCRAPPED', 'RETURNED', 'QUARANTINED'),
      allowNull: false,
      defaultValue: 'IN_STOCK',
      comment: 'Current status of the serial number'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true,
      comment: 'Current storage location'
    },
    reference_type: {
      type: DataTypes.ENUM('GRN', 'GATE_ENTRY', 'MATERIAL_ISSUE', 'PRODUCTION_ORDER', 'DELIVERY', 'SALES_INVOICE', 'SCRAP', 'RETURN', 'TRANSFER', 'ADJUSTMENT', 'OTHER'),
      allowNull: true,
      comment: 'Type of transaction that last moved this serial number'
    },
    reference_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'ID of the transaction that last moved this serial number'
    },
    reference_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Reference number of the transaction'
    },
    transaction_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Date of last transaction'
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      },
      comment: 'Production Order if serial is in production'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      },
      comment: 'Customer if serial is delivered'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    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: 'serial_number_tracking',
    indexes: [
      {
        fields: ['company_id', 'plant_id']
      },
      {
        fields: ['material_id']
      },
      {
        unique: true,
        fields: ['serial_number']
      },
      {
        fields: ['batch_number']
      },
      {
        fields: ['current_status']
      },
      {
        fields: ['reference_type', 'reference_id']
      },
      {
        fields: ['production_order_id']
      }
    ]
  });

  SerialNumberTracking.associate = function(models) {
    SerialNumberTracking.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    SerialNumberTracking.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    SerialNumberTracking.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    SerialNumberTracking.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    SerialNumberTracking.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    SerialNumberTracking.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    SerialNumberTracking.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return SerialNumberTracking;
};

