module.exports = (sequelize, DataTypes) => {
  const InventoryGoodsReceipt = sequelize.define('InventoryGoodsReceipt', {
    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'
      }
    },
    movement_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    movement_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    movement_type: {
      type: DataTypes.STRING(10),
      allowNull: false,
      defaultValue: '101',
      comment: 'SAP Movement Type: 101=Goods Receipt'
    },
    reference_type: {
      type: DataTypes.ENUM('PRODUCTION_ORDER', 'RETURN', 'ADJUSTMENT', 'TRANSFER', 'OTHER'),
      allowNull: true
    },
    reference_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: 'Reference document number'
    },
    storage_location: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    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: 'inventory_goods_receipt',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['movement_number']
      },
      {
        fields: ['movement_date']
      },
      {
        fields: ['status']
      }
    ]
  });

  InventoryGoodsReceipt.associate = function(models) {
    InventoryGoodsReceipt.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    InventoryGoodsReceipt.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    InventoryGoodsReceipt.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    InventoryGoodsReceipt.hasMany(models.InventoryGoodsReceiptItem, { foreignKey: 'movement_id', as: 'items' });
    InventoryGoodsReceipt.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    InventoryGoodsReceipt.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    InventoryGoodsReceipt.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return InventoryGoodsReceipt;
};

