#!/usr/bin/env python3
"""
Fix the All Courses panel - it's inside a hidden parent div.
The JS openPanel needs to also unhide ancestor hidden divs.
"""

with open('/Applications/XAMPP/xamppfiles/htdocs/aaa/header.php', 'r', encoding='utf-8') as f:
    content = f.read()

# Replace the openPanel function with one that also walks ancestors to remove hidden
OLD_OPEN_PANEL = '''      function openPanel() {
        isOpen = true;
        var headerEl = document.querySelector('header');
        var headerBottom = headerEl ? headerEl.getBoundingClientRect().bottom : 112;
        domainPanel.style.cssText = [
          'display:block',
          'position:fixed',
          'top:' + headerBottom + 'px',
          'left:0',
          'right:0',
          'width:100%',
          'z-index:99999',
          'background:white',
          'box-shadow:0 8px 32px rgba(0,0,0,0.18)',
          'overflow:hidden',
          'max-height:calc(100vh - ' + headerBottom + 'px)'
        ].join(';');

        var inner = domainPanel.querySelector('[style*="height:calc"]');
        if (inner) {
          inner.style.height = 'auto';
          inner.style.maxHeight = 'calc(100vh - ' + headerBottom + 'px)';
          inner.style.overflowY = 'auto';
          inner.style.display = 'flex';
        }
      }'''

NEW_OPEN_PANEL = '''      function openPanel() {
        isOpen = true;
        var headerEl = document.querySelector('header');
        var headerBottom = headerEl ? headerEl.getBoundingClientRect().bottom : 112;

        // Walk ALL ancestors between allCoursesBtn and domainPanel and remove "hidden" class
        // The panel's direct parent may still have display:none via "hidden" Tailwind class
        var el = domainPanel;
        var safety = 0;
        while (el && safety < 15) {
          if (el.classList && el.classList.contains('hidden')) {
            el.classList.remove('hidden');
            el.style.display = 'block';
          }
          if (el === document.body) break;
          el = el.parentElement;
          safety++;
        }

        domainPanel.style.cssText = [
          'display:block !important',
          'position:fixed',
          'top:' + headerBottom + 'px',
          'left:0',
          'right:0',
          'width:100%',
          'z-index:999999',
          'background:white',
          'box-shadow:0 8px 32px rgba(0,0,0,0.22)',
          'overflow:hidden',
          'max-height:calc(100vh - ' + headerBottom + 'px)'
        ].join(';');

        var inner = domainPanel.querySelector('[style*="height:calc"]');
        if (inner) {
          inner.style.height = 'auto';
          inner.style.maxHeight = 'calc(100vh - ' + headerBottom + 'px)';
          inner.style.overflowY = 'auto';
          inner.style.display = 'flex';
        }
      }'''

if OLD_OPEN_PANEL in content:
    content = content.replace(OLD_OPEN_PANEL, NEW_OPEN_PANEL)
    print("openPanel replaced successfully!")
else:
    print("ERROR: Old openPanel not found!")
    # Show the current function
    idx = content.find('function openPanel()')
    if idx > -1:
        print("Current openPanel at", idx, ":")
        print(content[idx:idx+600])

# Also fix closePanel to re-add hidden classes properly 
# Actually we just need to set our panel display:none - not re-add hidden to parents
# The parents should stay visible since they are the nav structure

with open('/Applications/XAMPP/xamppfiles/htdocs/aaa/header.php', 'w', encoding='utf-8') as f:
    f.write(content)
print("Done!")
