from app.core.database import SessionLocal
from app.models.inventory import Product
from app.models.purchases import PurchaseItem

db = SessionLocal()
total = db.query(Product).count()
with_qty = db.query(Product).filter(Product.quantity > 0).count()
print("products_total=%s" % total)
print("products_qty_gt_0=%s" % with_qty)
for pi in db.query(PurchaseItem).order_by(PurchaseItem.id.desc()).limit(8).all():
    p = db.query(Product).filter(Product.id == pi.product_id).first() if pi.product_id else None
    print(
        "item id=%s qty=%s product_id=%s product_qty=%s name=%s is_inv=%s"
        % (
            pi.id,
            pi.quantity,
            pi.product_id,
            getattr(p, "quantity", None),
            getattr(p, "name", None),
            getattr(pi, "is_inventory", None),
        )
    )
db.close()
