#!/bin/bash
# Complete Services & Inventory Verification

echo "╔════════════════════════════════════════════════════════╗"
echo "║   Services & Inventory Complete Verification          ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""

cd /media/pts/Website/PromptTech_Solution_Site/backend
source venv/bin/activate
TOKEN=$(python test_upload.py 2>/dev/null | grep "eyJ" | head -1 | tr -d ' ')

# 1. Test Services Loading
echo "🔹 Services API Test:"
SERVICES_COUNT=$(curl -s http://localhost:8181/api/services | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))')
echo "   ✅ Total services loaded: $SERVICES_COUNT"
echo ""

# 2. Test Services by Category
echo "🔹 Services by Category:"
for cat in "repair" "data" "software" "setup"; do
    COUNT=$(curl -s "http://localhost:8181/api/services?category=$cat" | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))')
    echo "   • $cat: $COUNT services"
done
echo ""

# 3. Test Inventory Loading
echo "🔹 Inventory Management Test:"
INVENTORY=$(curl -s http://localhost:8181/api/admin/inventory -H "Authorization: Bearer $TOKEN")
TOTAL_PRODUCTS=$(echo "$INVENTORY" | python3 -c 'import sys,json; print(len(json.load(sys.stdin)))')
LOW_STOCK=$(echo "$INVENTORY" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(sum(1 for p in d if p["is_low_stock"]))')

echo "   ✅ Total products: $TOTAL_PRODUCTS"
echo "   ⚠️  Low stock items: $LOW_STOCK"
echo ""

# 4. Show Inventory Details
echo "🔹 Inventory Details (Top 5):"
echo "$INVENTORY" | python3 -c '
import sys, json
products = json.load(sys.stdin)
for p in products[:5]:
    status = "🔴 LOW STOCK" if p["is_low_stock"] else "✅ IN STOCK"
    print(f"   {status} | {p[\"name\"]}: {p[\"stock\"]} units (threshold: {p[\"low_stock_threshold\"]})")
'
echo ""

# 5. Test Inventory Adjustment
echo "🔹 Testing Inventory Adjustment:"
FIRST_PRODUCT_ID=$(echo "$INVENTORY" | python3 -c 'import sys,json; print(json.load(sys.stdin)[0]["id"])')
ADJUST_RESULT=$(curl -s -X POST "http://localhost:8181/api/admin/inventory/$FIRST_PRODUCT_ID/adjust" \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d '{"quantity_change": 5, "notes": "Test inventory adjustment"}')

NEW_STOCK=$(echo "$ADJUST_RESULT" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get("new_stock", "N/A"))')
echo "   ✅ Inventory adjusted, new stock: $NEW_STOCK"
echo ""

# 6. Verify Database Persistence
echo "🔹 Verifying Database Persistence:"
PERSISTED_STOCK=$(curl -s http://localhost:8181/api/admin/inventory -H "Authorization: Bearer $TOKEN" | python3 -c "import sys,json; products=json.load(sys.stdin); print([p['stock'] for p in products if p['id']=='$FIRST_PRODUCT_ID'][0])")
echo "   ✅ Stock persisted in database: $PERSISTED_STOCK"
echo ""

# 7. Frontend Status
echo "🔹 Frontend Status:"
FRONTEND_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5300)
if [ "$FRONTEND_STATUS" == "200" ]; then
    echo "   ✅ Frontend running (HTTP 200)"
else
    echo "   ❌ Frontend issue (HTTP $FRONTEND_STATUS)"
fi
echo ""

# Summary
echo "╔════════════════════════════════════════════════════════╗"
echo "║                   Test Summary                         ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
echo "✅ Services Loading: $SERVICES_COUNT services"
echo "✅ Inventory Management: $TOTAL_PRODUCTS products"
echo "✅ Low Stock Detection: Working ($LOW_STOCK items)"
echo "✅ Inventory Adjustments: Working"
echo "✅ Database Persistence: Working"
echo "✅ Frontend Connection: Working"
echo ""
echo "🎉 All services and inventory features functioning correctly!"
echo ""
