import os
import re

base_dir = '/Applications/XAMPP/xamppfiles/htdocs/ecom/resources/views'
main_layout_path = os.path.join(base_dir, 'layouts/main.blade.php')

items = [
    ("Upcoming on 2025", "conference", "upcoming-2025"),
    ("Upcoming on 2026", "conference", "upcoming-2026"),
    ("Upcoming on 2027", "conference", "upcoming-2027"),
    ("Co-chairs & Keynote", "about", "co-chairs-keynote"),
    ("Presenters - Upcoming", "about", "presenters-upcoming"),
    ("Scientific Committee", "about", "scientific-committee"),
    ("About GARI", "about", "about-gari"),
    ("Advisory Team", "about", "advisory-team"),
    ("GARI Publications", "about", "gari-publications"),
    ("Journal Partners", "about", "journal-partners"),
    ("Academic Partners", "about", "academic-partners"),
    ("Country Ambassador", "about", "country-ambassador"),
    ("Organizing Teams", "about", "organizing-teams"),
    ("CSR Project", "about", "csr-project"),
    ("Presenter / Participant", "apply", "presenter"),
    ("Co-Chair / Keynote", "apply", "co-chair"),
    ("Scientific Reviewer", "apply", "scientific-reviewer"),
    ("Journal Editorial Board", "apply", "journal-editorial-board"),
    ("Apply for Journal Editorial Board", "apply", "journal-editorial-board"),
    ("Media Partner", "apply", "media-partner"),
    ("Media Partner Apply", "apply", "media-partner"),
    ("Sponsor", "apply", "sponsor"),
    ("Sponsor Apply", "apply", "sponsor"),
    ("Library", "library", "library-index"),
    ("Past Proceedings", "library", "past-proceedings"),
    ("Abstract Guideline", "library", "abstract-guideline"),
    ("Full Paper Guideline", "library", "full-paper-guideline"),
    ("Past Conferences", "history", "past-conferences"),
    ("Past Speakers", "history", "past-speakers"),
    ("Presenters", "history", "presenters"),
    ("Award Winners", "history", "award-winners"),
    ("Video Gallery", "history", "video-gallery"),
    ("Image Gallery", "history", "image-gallery"),
    ("Contact Us", "contact", "contact-us"),
    ("Alumini", "contact", "alumini"),
    ("Accommodation", "contact", "accommodation"),
    ("Visit Sri Lanka", "contact", "visit-sri-lanka"),
    ("Visa Apply", "contact", "visa-apply"),
    ("Venue", "contact", "venue"),
    ("FAQ", "contact", "faq"),
    ("Donations", "contact", "donations"),
    ("Sign In", "auth", "login"),
    ("Register", "auth", "register")
]

with open(main_layout_path, 'r', encoding='utf-8') as f:
    content = f.read()

def replace_a_tags(match):
    tag_html = match.group(0)
    if 'href="#"' in tag_html or 'href=""' in tag_html or "href='#'" in tag_html:
        matched_item = None
        for text, category, page in items:
            inner_text = match.group(2)
            if text in inner_text:
                matched_item = (text, category, page)
                break
        
        if matched_item:
            text, category, page = matched_item
            if category == "auth":
                if page == "login":
                    route_str = "{{ route('login') }}"
                else:
                    route_str = "{{ route('register') }}"
            else:
                route_str = f"{{{{ route('main.page', ['category' => '{category}', 'page' => '{page}']) }}}}"
            new_tag = re.sub(r'href="#"', f'href="{route_str}"', tag_html, count=1)
            return new_tag
        
    return tag_html

# Correctly split the tag matching capturing group 2 as the inner html.
content = re.sub(r'(<a\b[^>]*>)(.*?)(</a>)', replace_a_tags, content, flags=re.IGNORECASE | re.DOTALL)

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

print("HTML modified safely.")
