module.exports = (sequelize, DataTypes) => {
  const PurchaseOrderItem = sequelize.define('PurchaseOrderItem', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'PurchaseOrder',
        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
    },
    received_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0.000,
      comment: 'Quantity received against this PO item'
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    unit_price: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false
    },
    discount_percent: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0
    },
    tax_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    cgst_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'CGST rate in percentage'
    },
    cgst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'CGST amount'
    },
    sgst_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'SGST rate in percentage'
    },
    sgst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'SGST amount'
    },
    igst_rate: {
      type: DataTypes.DECIMAL(5, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'IGST rate in percentage'
    },
    igst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'IGST amount'
    },
    storage_location_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'StorageLocation',
        key: 'id'
      },
      comment: 'Default storage location for this PO line (copied from Material, editable by user)'
    },
    line_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    hsn_code: {
      type: DataTypes.STRING(8),
      allowNull: true
    },
    delivery_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    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: 'purchase_order_item',
    indexes: [
      {
        fields: ['po_id']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['storage_location_id']
      }
    ]
  });

  PurchaseOrderItem.associate = function(models) {
    PurchaseOrderItem.belongsTo(models.PurchaseOrder, { foreignKey: 'po_id', as: 'purchaseOrder' });
    PurchaseOrderItem.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    PurchaseOrderItem.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
    PurchaseOrderItem.belongsTo(models.StorageLocation, { foreignKey: 'storage_location_id', as: 'storageLocation' });
  };

  return PurchaseOrderItem;
};

