#!/bin/bash

echo "=========================================="
echo "  CART & WISHLIST DRAWER TEST SUITE"
echo "=========================================="
echo ""

# Test 1: Check CSS is correct
echo "TEST 1: Verifying CSS for cart drawer..."
CART_CSS=$(curl -s http://localhost:5000/assets/css/modern-theme.css | grep -A 15 "\.cart-drawer {")
if echo "$CART_CSS" | grep -q "right: -400px" && echo "$CART_CSS" | grep -q "visibility: hidden"; then
    echo "✓ PASS: Cart drawer CSS is correct (right: -400px, visibility: hidden)"
else
    echo "✗ FAIL: Cart drawer CSS is incorrect"
    echo "$CART_CSS"
fi

echo ""
echo "TEST 2: Verifying CSS for wishlist drawer..."
WISHLIST_CSS=$(curl -s http://localhost:5000/assets/css/modern-theme.css | grep -A 15 "\.wishlist-drawer {")
if echo "$WISHLIST_CSS" | grep -q "right: -400px" && echo "$WISHLIST_CSS" | grep -q "visibility: hidden"; then
    echo "✓ PASS: Wishlist drawer CSS is correct (right: -400px, visibility: hidden)"
else
    echo "✗ FAIL: Wishlist drawer CSS is incorrect"
    echo "$WISHLIST_CSS"
fi

echo ""
echo "TEST 3: Checking HTML on all pages..."
for page in "" "shop" "portfolio" "about" "blog" "contact" "product"; do
    CART_CLASS=$(curl -s "http://localhost:5000/${page}" 2>/dev/null | grep -o 'class="cart-drawer[^"]*"' | head -1)
    WISHLIST_CLASS=$(curl -s "http://localhost:5000/${page}" 2>/dev/null | grep -o 'class="wishlist-drawer[^"]*"' | head -1)
    
    if [[ "$CART_CLASS" == 'class="cart-drawer"' ]] && [[ "$WISHLIST_CLASS" == 'class="wishlist-drawer"' ]]; then
        echo "✓ PASS: /${page} - No .open class on drawers"
    else
        echo "✗ FAIL: /${page} - Unexpected classes: $CART_CLASS $WISHLIST_CLASS"
    fi
done

echo ""
echo "TEST 4: Verifying JavaScript files are loading..."
JS_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5000/assets/js/modern-theme.js)
if [ "$JS_STATUS" == "200" ]; then
    echo "✓ PASS: JavaScript file loads successfully (HTTP $JS_STATUS)"
else
    echo "✗ FAIL: JavaScript file failed to load (HTTP $JS_STATUS)"
fi

echo ""
echo "TEST 5: Checking mobile responsive styles..."
MOBILE_CSS=$(curl -s http://localhost:5000/assets/css/modern-theme.css | grep -A 10 "@media (max-width: 480px)")
if echo "$MOBILE_CSS" | grep -q "width: 100%" && echo "$MOBILE_CSS" | grep -q "right: -100%"; then
    echo "✓ PASS: Mobile styles correctly set (100% width, right: -100%)"
else
    echo "✗ FAIL: Mobile styles are incorrect"
fi

echo ""
echo "TEST 6: Verifying PM2 process status..."
PM2_STATUS=$(pm2 jlist | jq -r '.[0].pm2_env.status')
if [ "$PM2_STATUS" == "online" ]; then
    echo "✓ PASS: Server is running (status: $PM2_STATUS)"
else
    echo "✗ FAIL: Server status is $PM2_STATUS"
fi

echo ""
echo "=========================================="
echo "  TEST SUMMARY"
echo "=========================================="
echo "All tests completed. Review results above."
echo ""
echo "MANUAL TEST INSTRUCTIONS:"
echo "1. Open http://localhost:5000 in your browser"
echo "2. Hard refresh (Ctrl+F5 or Cmd+Shift+R)"
echo "3. Verify NO cart or wishlist visible on page load"
echo "4. Click cart icon - drawer should slide in from right"
echo "5. Click outside or close button - drawer should slide out"
echo "6. Repeat for wishlist icon"
echo "7. Test on mobile view (resize browser < 480px)"
echo "=========================================="
