import os
import re

html_files = [f for f in os.listdir('.') if f.endswith('.html')]

# Clean nav styling for all files
nav_clean_css = """
/* ============================================================
   UNIFIED RESPONSIVE NAVIGATION BREAKPOINT (1200px)
   ============================================================ */
@media (min-width: 1201px) {
    .fs-desktop-nav {
        display: flex !important;
        gap: clamp(0.75rem, 1.3vw, 1.75rem) !important;
        align-items: center !important;
    }
    .fs-desktop-nav a,
    .fs-desktop-nav button,
    .fs-desktop-nav .fs-mega-toggle {
        font-size: clamp(0.82rem, 0.88vw, 0.9rem) !important;
        white-space: nowrap !important;
    }
    .fs-mobile-toggle,
    .fs-mobile-nav,
    .mobile-nav-drawer {
        display: none !important;
    }
    .fs-logo-text {
        display: flex !important;
    }
}

@media (max-width: 1200px) {
    .fs-desktop-nav,
    .fs-mega-menu {
        display: none !important;
    }
    .fs-mobile-toggle {
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        width: 44px !important;
        height: 44px !important;
        padding: 0 !important;
        background: transparent !important;
        border: none !important;
        cursor: pointer !important;
        z-index: 120 !important;
    }
    .fs-mobile-toggle span {
        display: flex !important;
        flex-direction: column !important;
        justify-content: space-between !important;
        width: 24px !important;
        height: 17px !important;
    }
    .fs-mobile-toggle span span {
        display: block !important;
        width: 100% !important;
        height: 2.5px !important;
        background-color: #ffffff !important;
        border-radius: 2px !important;
    }
    .fs-header-actions {
        order: 1 !important;
        display: flex !important;
        align-items: center !important;
    }
    .fs-logo {
        order: 2 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: flex-end !important;
    }
    .fs-logo img {
        height: 38px !important;
        width: auto !important;
        max-width: 48px !important;
    }
    .fs-logo-text {
        display: none !important;
    }
}
"""

# Update CSS files
for css_path in [r'css\styles.css', r'css\design-system.css']:
    if os.path.exists(css_path):
        with open(css_path, 'r', encoding='utf-8') as f:
            c = f.read()
        # Remove previous conflicting 991px/1200px blocks
        c = re.sub(r'/\* =+ RESPONSIVE TABLET & DESKTOP NAVIGATION BREAKPOINT.*?/\* =+', '/* =+', c, flags=re.DOTALL)
        c = re.sub(r'/\* =+ UNIFIED RESPONSIVE NAVIGATION BREAKPOINT.*', '', c, flags=re.DOTALL)
        c += "\n" + nav_clean_css
        with open(css_path, 'w', encoding='utf-8') as f:
            f.write(c)

# Clean and update each HTML file
for fname in html_files:
    with open(fname, 'r', encoding='utf-8') as f:
        c = f.read()

    # 1. Fix duplicate publications in desktop nav
    # Find desktop nav
    desktop_match = re.search(r'(<nav class="fs-desktop-nav">)(.*?)(</nav>)', c, re.DOTALL)
    if desktop_match:
        nav_inner = desktop_match.group(2)
        # Remove all instances of publications
        nav_inner_clean = re.sub(r'<a href="publications\.html"[^>]*>Publications</a>\s*', '', nav_inner)
        active_str = ' class="active"' if fname == 'publications.html' else ''
        # Insert exactly one publications link right before news.html
        nav_inner_fixed = re.sub(
            r'(<a href="news\.html"[^>]*>News</a>)',
            f'<a href="publications.html"{active_str}>Publications</a>\n      \\1',
            nav_inner_clean
        )
        c = c[:desktop_match.start(2)] + nav_inner_fixed + c[desktop_match.end(2):]

    # 2. Fix duplicate publications in mobile nav
    mobile_match = re.search(r'(<nav class="fs-mobile-nav"[^>]*>)(.*?)(</nav>)', c, re.DOTALL)
    if mobile_match:
        mob_inner = mobile_match.group(2)
        mob_inner_clean = re.sub(r'<a href="publications\.html"[^>]*>Publications</a>\s*', '', mob_inner)
        mob_inner_fixed = re.sub(
            r'(<a href="news\.html"[^>]*>News</a>)',
            '<a href="publications.html">Publications</a>\n  \\1',
            mob_inner_clean
        )
        c = c[:mobile_match.start(2)] + mob_inner_fixed + c[mobile_match.end(2):]

    # 3. Fix duplicate in mobile-nav-drawer if present
    drawer_match = re.search(r'(<div class="drawer-nav">)(.*?)(</div>\s*<!-- Right-hand)', c, re.DOTALL)
    if drawer_match:
        dr_inner = drawer_match.group(2)
        dr_inner_clean = re.sub(r'<a href="publications\.html"[^>]*>Publications</a>\s*', '', dr_inner)
        dr_inner_fixed = re.sub(
            r'(<a href="news\.html"[^>]*>News</a>)',
            '<a href="publications.html" style="color: #fff; font-weight: 600; text-decoration: none; font-size: 1.1rem;">Publications</a>\n        \\1',
            dr_inner_clean
        )
        c = c[:drawer_match.start(2)] + dr_inner_fixed + c[drawer_match.end(2):]

    # 4. Remove duplicate in footer
    footer_match = re.search(r'(<ul[^>]*style="list-style:\s*none;[^"]*">)(.*?)(</ul>)', c, re.DOTALL)
    if footer_match:
        ft_inner = footer_match.group(2)
        # Check if publications is repeated
        if ft_inner.count('publications.html') > 1:
            ft_inner_clean = re.sub(r'<li><a href="publications\.html"[^>]*>Publications</a></li>\s*', '', ft_inner)
            ft_inner_fixed = re.sub(
                r'(<li><a href="gallery\.html"[^>]*>Gallery</a></li>)',
                '\\1\n                                <li><a href="publications.html" style="color: #a8a29e; text-decoration: none; transition: color 0.3s ease;" onmouseover="this.style.color=\'#e6c888\'" onmouseout="this.style.color=\'#a8a29e\'">Publications</a></li>',
                ft_inner_clean
            )
            c = c[:footer_match.start(2)] + ft_inner_fixed + c[footer_match.end(2):]

    # 5. Remove any old 992px media query hiding fs-mobile-toggle inside inline style tag
    c = re.sub(
        r'@media\s*\(\s*min-width:\s*992px\s*\)\s*\{\s*\.fs-desktop-nav\s*\{[^}]*\}\s*\.fs-mobile-toggle\s*\{[^}]*\}\s*\.fs-mobile-nav\s*\{[^}]*\}\s*\}',
        '',
        c
    )
    
    # 6. Ensure custom-menu-active-styles has nav_clean_css
    if 'id="custom-menu-active-styles"' in c:
        # replace previous responsive block
        c = re.sub(r'/\* =+ UNIFIED RESPONSIVE NAVIGATION BREAKPOINT.*?/\* =+', '/* =+', c, flags=re.DOTALL)
        c = re.sub(r'/\* =+ RESPONSIVE TABLET & DESKTOP NAVIGATION BREAKPOINT.*?/\* =+', '/* =+', c, flags=re.DOTALL)
        c = c.replace(
            '<style id="custom-menu-active-styles">',
            f'<style id="custom-menu-active-styles">\n{nav_clean_css}\n'
        )

    with open(fname, 'w', encoding='utf-8') as f:
        f.write(c)

print(f"Cleaned up duplicates and fixed tablet hamburger menu visibility across all {len(html_files)} files!")
