#!/bin/bash
# Complete Admin Panel Fix Verification

echo "=========================================="
echo "  Admin Panel Fix Verification"
echo "=========================================="

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo ""
echo "1️⃣  Checking deployed files..."

# Check auth.js exists
if [ -f "/var/www/skyartshop/admin/js/auth.js" ]; then
    echo -e "${GREEN}✓ auth.js is deployed${NC}"
else
    echo -e "${RED}✗ auth.js is missing${NC}"
fi

# Check that admin pages include auth.js
PAGES=("dashboard.html" "products.html" "homepage.html" "blog.html")
for page in "${PAGES[@]}"; do
    if grep -q "auth\.js" "/var/www/skyartshop/admin/$page"; then
        echo -e "${GREEN}✓ $page includes auth.js${NC}"
    else
        echo -e "${RED}✗ $page missing auth.js${NC}"
    fi
done

echo ""
echo "2️⃣  Testing HTTP accessibility..."

# Test auth.js is accessible
STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5000/admin/js/auth.js)
if [ "$STATUS" == "200" ]; then
    echo -e "${GREEN}✓ auth.js accessible via HTTP${NC}"
else
    echo -e "${RED}✗ auth.js not accessible (HTTP $STATUS)${NC}"
fi

# Test admin pages are accessible
for page in "${PAGES[@]}"; do
    STATUS=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:5000/admin/$page")
    if [ "$STATUS" == "200" ] || [ "$STATUS" == "304" ]; then
        echo -e "${GREEN}✓ $page accessible${NC}"
    else
        echo -e "${RED}✗ $page not accessible (HTTP $STATUS)${NC}"
    fi
done

echo ""
echo "3️⃣  Testing API endpoints..."

# Test session endpoint
STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5000/api/admin/session)
if [ "$STATUS" == "401" ] || [ "$STATUS" == "200" ]; then
    echo -e "${GREEN}✓ Session API working (HTTP $STATUS)${NC}"
else
    echo -e "${RED}✗ Session API issue (HTTP $STATUS)${NC}"
fi

echo ""
echo "=========================================="
echo "  ✅ Verification Complete!"
echo "=========================================="
echo ""
echo -e "${YELLOW}⚠️  IMPORTANT: Clear Your Browser Cache!${NC}"
echo ""
echo "Choose your browser:"
echo ""
echo "🌐 Chrome/Edge:"
echo "   1. Press Ctrl+Shift+Delete"
echo "   2. Select 'All time'"
echo "   3. Check 'Cached images and files'"
echo "   4. Click 'Clear data'"
echo ""
echo "🦊 Firefox:"
echo "   1. Press Ctrl+Shift+Delete"
echo "   2. Time range: 'Everything'"
echo "   3. Check 'Cache'"
echo "   4. Click 'Clear Now'"
echo ""
echo "Or use incognito/private browsing mode:"
echo "   Chrome/Edge: Ctrl+Shift+N"
echo "   Firefox: Ctrl+Shift+P"
echo ""
echo "Then test:"
echo "   1. Go to: http://localhost:5000/admin/login.html"
echo "   2. Login with your credentials"
echo "   3. Click on different sections in left panel"
echo "   4. Click on live tiles in dashboard"
echo "   5. You should stay logged in!"
echo ""
