#!/bin/bash

# Script to verify modal setup on all HTML pages

echo "🔍 Modal Implementation Verification Report"
echo "==========================================="
echo ""

CHECKMARK="✅"
CROSS="❌"

# Check 1: Modal HTML file exists
if [ -f "222/blocks/_booking-modal.html" ]; then
    echo "$CHECKMARK Modal HTML file exists"
    MODAL_SIZE=$(wc -c < "222/blocks/_booking-modal.html")
    echo "   Size: $MODAL_SIZE bytes"
else
    echo "$CROSS Modal HTML file NOT found"
fi

# Check 2: Main.js exists and contains modal loading code
if [ -f "222/assets/js/main.js" ]; then
    echo "$CHECKMARK Main.js exists"
    if grep -q "initBookingModal" "222/assets/js/main.js"; then
        echo "   ✓ Contains initBookingModal function"
    else
        echo "   ✗ Missing initBookingModal function"
    fi
    if grep -q "setupBookingModalHandlers" "222/assets/js/main.js"; then
        echo "   ✓ Contains setupBookingModalHandlers function"
    else
        echo "   ✗ Missing setupBookingModalHandlers function"
    fi
    if grep -q "window.openBookingModal" "222/assets/js/main.js"; then
        echo "   ✓ Exports openBookingModal to global window"
    else
        echo "   ✗ Missing global openBookingModal export"
    fi
else
    echo "$CROSS Main.js NOT found"
fi

# Check 3: Verify modal has correct ID
if grep -q 'id="booking-modal"' "222/blocks/_booking-modal.html"; then
    echo "$CHECKMARK Modal has correct ID (booking-modal)"
else
    echo "$CROSS Modal ID mismatch"
fi

# Check 4: Verify modal uses inline styles (position: fixed)
if grep -q 'position: fixed' "222/blocks/_booking-modal.html"; then
    echo "$CHECKMARK Modal uses position: fixed (Safari compatible)"
else
    echo "$CROSS Modal missing position: fixed style"
fi

# Check 5: Verify CSS backup styles exist
if grep -q '#booking-modal' "222/assets/css/style.css"; then
    echo "$CHECKMARK CSS styles defined for modal"
else
    echo "$CROSS CSS styles missing"
fi

# Check 6: Count HTML pages
HTML_FILES=$(find 222 -maxdepth 1 -name "*.html" ! -name "test-*.html" ! -name "check-*.html" | wc -l)
echo "$CHECKMARK Found $HTML_FILES main HTML pages"

# Check 7: Verify no orphaned modal containers
ORPHANED=$(grep -r "booking-modal-container" 222/*.html 2>/dev/null | wc -l)
if [ "$ORPHANED" -eq 0 ]; then
    echo "$CHECKMARK No orphaned modal containers found"
else
    echo "$CROSS Found $ORPHANED orphaned modal containers"
fi

# Check 8: Test modal buttons exist
if grep -q 'onclick="openBookingModal()' "222/blocks/_header.html"; then
    echo "$CHECKMARK Booking button exists in header"
else
    echo "$CROSS Booking button missing in header"
fi

# Check 9: Verify header loads in main.js
if grep -q "loadComponent('header-container'" "222/assets/js/main.js"; then
    echo "$CHECKMARK Header component loader configured"
else
    echo "$CROSS Header component loader missing"
fi

# Check 10: Count test files
TEST_FILES=$(ls -1 222/test-*.html 222/check-*.html 2>/dev/null | wc -l)
echo "$CHECKMARK Created $TEST_FILES test/verification files"

echo ""
echo "==========================================="
echo "✅ Verification Complete!"
echo ""
echo "To test the modal:"
echo "  1. Open http://localhost:8000/index.html"
echo "  2. Click the 'Забронировать тур' button in header"
echo "  3. Modal should appear centered with form"
echo ""
echo "Debug pages:"
echo "  - http://localhost:8000/check-modal.html - Automated checks"
echo "  - http://localhost:8000/test-modal-integration.html - Interactive test"
