#!/bin/bash

echo "========================================="
echo "TechZone Admin Features Verification"
echo "========================================="
echo ""

# Login as admin
echo "1. Testing Admin Login..."
LOGIN_RESPONSE=$(curl -s -X POST http://localhost:8181/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"admin@techzone.com","password":"admin123"}')

TOKEN=$(echo $LOGIN_RESPONSE | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])" 2>/dev/null)

if [ -z "$TOKEN" ]; then
  echo "❌ Login failed"
  exit 1
fi
echo "✅ Admin login successful"
echo ""

# Test Dashboard
echo "2. Testing Dashboard Stats..."
DASHBOARD=$(curl -s http://localhost:8181/api/admin/dashboard -H "Authorization: Bearer $TOKEN")
echo "$DASHBOARD" | python3 -c "
import sys, json
data = json.load(sys.stdin)
print('✅ Dashboard endpoint working')
print(f\"   - Total Products: {data['stats']['total_products']}\")
print(f\"   - Total Services: {data['stats']['total_services']}\")
print(f\"   - Total Orders: {data['stats']['total_orders']}\")
print(f\"   - Total Revenue: \${data['stats']['total_revenue']:.2f}\")
"
echo ""

# Test Products CRUD
echo "3. Testing Products Management..."
curl -s http://localhost:8181/api/admin/products -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ Get Products (Read)"
echo ""

# Test Services CRUD
echo "4. Testing Services Management..."
curl -s http://localhost:8181/api/admin/services -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ Get Services (Read)"
echo ""

# Test Orders
echo "5. Testing Orders Management..."
curl -s http://localhost:8181/api/admin/orders -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ Get Orders"
echo ""

# Test Inventory
echo "6. Testing Inventory with Low Stock Alerts..."
INVENTORY=$(curl -s http://localhost:8181/api/admin/inventory -H "Authorization: Bearer $TOKEN")
echo "$INVENTORY" | python3 -c "
import sys, json
data = json.load(sys.stdin)
low_stock = [p for p in data if p.get('is_low_stock', False)]
print(f'✅ Inventory endpoint working')
print(f'   - Total products tracked: {len(data)}')
print(f'   - Low stock items: {len(low_stock)}')
"
echo ""

# Test Bookings
echo "7. Testing Service Bookings..."
curl -s http://localhost:8181/api/admin/bookings -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ Get Bookings"
echo ""

# Test Reports
echo "8. Testing Sales Reports..."
for period in daily weekly monthly; do
  curl -s "http://localhost:8181/api/admin/reports/sales?period=$period" -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ ${period^} sales report"
done
echo ""

# Test Exports
echo "9. Testing CSV & PDF Export..."
curl -s "http://localhost:8181/api/admin/reports/export/csv?report_type=sales&period=monthly" -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ CSV Export"
curl -s "http://localhost:8181/api/admin/reports/export/pdf?report_type=sales&period=monthly" -H "Authorization: Bearer $TOKEN" > /dev/null && echo "✅ PDF Export"
echo ""

echo "========================================="
echo "✅ All Admin Features Verified!"
echo "========================================="
echo ""
echo "Frontend Routes:"
echo "  • /login - Login with admin credentials"
echo "  • /admin - Access admin dashboard"
echo ""
echo "Admin Credentials:"
echo "  Email: admin@techzone.com"
echo "  Password: admin123"
echo ""
echo "Available Features:"
echo "  ✓ Dashboard with stats & analytics"
echo "  ✓ Products management (CRUD)"
echo "  ✓ Services management (CRUD)"
echo "  ✓ Orders management & status updates"
echo "  ✓ Inventory with low stock alerts"
echo "  ✓ Service bookings management"
echo "  ✓ Sales reports (daily/weekly/monthly)"
echo "  ✓ CSV & PDF export"
echo ""
