module.exports = (sequelize, DataTypes) => {
  const GoodsReceiptNoteItem = sequelize.define('GoodsReceiptNoteItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    grn_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'GoodsReceiptNote',
        key: 'id'
      }
    },
    po_item_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseOrderItem',
        key: 'id'
      }
    },
    line_number: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: true
    },
    quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false
    },
    excess_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0,
      comment: 'Excess quantity beyond PO quantity for this GRN line'
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false
    },
    tax_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    line_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    batch_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    storage_location_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'StorageLocation',
        key: 'id'
      },
      comment: 'Storage location for this item (overrides header storage location if set)'
    },
    required_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      comment: 'Date by which the material was required (from Material Request -> PR -> PO -> GRN)'
    },
    line_weight_kg: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      comment: 'Total line weight in KG at transaction time'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'goods_receipt_note_item',
    indexes: [
      {
        fields: ['grn_id']
      },
      {
        fields: ['po_item_id']
      },
      {
        fields: ['material_id']
      }
    ]
  });

  GoodsReceiptNoteItem.associate = function(models) {
    GoodsReceiptNoteItem.belongsTo(models.GoodsReceiptNote, { foreignKey: 'grn_id', as: 'goodsReceiptNote' });
    GoodsReceiptNoteItem.belongsTo(models.PurchaseOrderItem, { foreignKey: 'po_item_id', as: 'purchaseOrderItem' });
    GoodsReceiptNoteItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    GoodsReceiptNoteItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    GoodsReceiptNoteItem.belongsTo(models.StorageLocation, { foreignKey: 'storage_location_id', as: 'storageLocation' });
    GoodsReceiptNoteItem.hasMany(models.GateEntryItem, { foreignKey: 'grn_item_id', as: 'gateEntryItems' });
  };

  return GoodsReceiptNoteItem;
};

