module.exports = (sequelize, DataTypes) => {
  const ProductionFGInward = sequelize.define(
    'ProductionFGInward',
    {
      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' }
      },
      fg_inward_number: {
        type: DataTypes.STRING(60),
        allowNull: false,
        unique: true
      },
      packing_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'ProductionPacking', key: 'id' }
      },
      reference_fg_inward_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'ProductionFGInward', key: 'id' }
      },
      route_card_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        references: { model: 'RouteCard', 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' }
      },
      storage_location_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: { model: 'StorageLocation', key: 'id' }
      },
      production_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      accepted_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      packed_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      inward_entry_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      balance_inward_qty: {
        type: DataTypes.DECIMAL(18, 3),
        allowNull: false,
        defaultValue: 0
      },
      inward_entry_date: {
        type: DataTypes.DATE,
        allowNull: false
      },
      status: {
        type: DataTypes.ENUM('PARTIAL_RECEIVED', 'FULLY_RECEIVED'),
        allowNull: false,
        defaultValue: 'PARTIAL_RECEIVED'
      },
      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_fg_inward',
      indexes: [
        { unique: true, fields: ['fg_inward_number'] },
        { fields: ['company_id', 'plant_id', 'fiscal_year_id'] },
        { fields: ['packing_id'] },
        { fields: ['reference_fg_inward_id'] },
        { fields: ['route_card_id'] },
        { fields: ['storage_location_id'] },
        { fields: ['status'] }
      ]
    }
  );

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

  return ProductionFGInward;
};
