#!/bin/bash

# Logout Confirmation Fix - Verification Script
# This script checks that all admin pages have been fixed

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  Logout Confirmation Fix - Verification"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo

# Define the files to check
JS_FILES=(
    "website/admin/js/blog.js"
    "website/admin/js/settings.js"
    "website/admin/js/pages.js"
    "website/admin/js/homepage.js"
    "website/admin/js/portfolio.js"
    "website/admin/js/products.js"
    "website/admin/js/users.js"
)

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

echo "🔍 Checking for duplicate logout functions..."
echo

all_clean=true

for file in "${JS_FILES[@]}"; do
    if [ ! -f "$file" ]; then
        echo -e "${RED}❌ File not found: $file${NC}"
        all_clean=false
        continue
    fi
    
    # Check if the file contains "async function logout"
    if grep -q "async function logout" "$file"; then
        echo -e "${RED}❌ FAIL: $file still has duplicate logout function${NC}"
        grep -n "async function logout" "$file"
        all_clean=false
    else
        echo -e "${GREEN}✅ PASS: $file - No duplicate logout function${NC}"
    fi
done

echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo

# Check that auth.js has the correct logout function
echo "🔍 Verifying auth.js has the proper logout function..."
if grep -q "window.logout = async function" "website/admin/js/auth.js"; then
    echo -e "${GREEN}✅ PASS: auth.js has proper window.logout function${NC}"
else
    echo -e "${RED}❌ FAIL: auth.js missing window.logout function${NC}"
    all_clean=false
fi

if grep -q "window.showLogoutConfirm" "website/admin/js/auth.js"; then
    echo -e "${GREEN}✅ PASS: auth.js has showLogoutConfirm function${NC}"
else
    echo -e "${RED}❌ FAIL: auth.js missing showLogoutConfirm function${NC}"
    all_clean=false
fi

echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo

# Check HTML files have auth.js included
echo "🔍 Verifying HTML pages include auth.js..."
HTML_FILES=(
    "website/admin/dashboard.html"
    "website/admin/settings.html"
    "website/admin/blog.html"
    "website/admin/users.html"
    "website/admin/products.html"
    "website/admin/homepage.html"
    "website/admin/portfolio.html"
    "website/admin/pages.html"
)

for file in "${HTML_FILES[@]}"; do
    if [ ! -f "$file" ]; then
        echo -e "${YELLOW}⚠️  File not found: $file${NC}"
        continue
    fi
    
    if grep -q "auth.js" "$file"; then
        echo -e "${GREEN}✅ PASS: $file includes auth.js${NC}"
    else
        echo -e "${RED}❌ FAIL: $file missing auth.js include${NC}"
        all_clean=false
    fi
done

echo
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo

# Final summary
if [ "$all_clean" = true ]; then
    echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${GREEN}  ✅ ALL CHECKS PASSED!${NC}"
    echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    echo "The logout confirmation fix is properly applied."
    echo "All pages should now show the confirmation dialog."
    echo
    echo "To test manually:"
    echo "  1. Login to http://localhost:5000/admin/login.html"
    echo "  2. Visit each admin page and click Logout"
    echo "  3. Verify the confirmation dialog appears"
    echo
    echo "Test pages available:"
    echo "  - http://localhost:5000/admin/test-logout-fix.html"
    echo
else
    echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${RED}  ❌ SOME CHECKS FAILED${NC}"
    echo -e "${RED}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo
    echo "Please review the errors above and fix them."
    exit 1
fi
