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", "apply", "media-partner"),
    ("Media Partner", "apply", "media-partner"),
    ("Sponsor Apply", "apply", "sponsor"),
    ("Sponsor", "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"),
    ("Visa Apply", "contact", "visa-apply"),
    ("Alumini", "contact", "alumini"),
    ("Accommodation", "contact", "accommodation"),
    ("Visit Sri Lanka", "contact", "visit-sri-lanka"),
    ("Venue", "contact", "venue"),
    ("FAQ", "contact", "faq"),
    ("Donations", "contact", "donations")
]

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

created_routes = set()

for text, category, page in items:
    route_str = f"{{{{ route('main.page', ['category' => '{category}', 'page' => '{page}']) }}}}"
    created_routes.add((category, page, text))
    
    pattern = r'<a\s+href="#"([^>]*)>(.*?)' + re.escape(text) + r'(.*?)</a>'
    
    def simple_replacer(match):
        return f'<a href="{route_str}"{match.group(1)}>{match.group(2)}{text}{match.group(3)}</a>'
        
    content = re.sub(pattern, simple_replacer, content, flags=re.IGNORECASE | re.DOTALL)

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

template_str = """<x-main-layout>
    <div class="bg-slate-900 py-16 relative overflow-hidden">
        <div class="absolute inset-0 opacity-10">
            <div class="absolute inset-0 bg-gradient-to-r from-brand-600 to-indigo-600 transform scale-150 -rotate-12 translate-y-1/4"></div>
        </div>
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 relative z-10 text-center">
            <span class="text-brand-400 font-bold tracking-wider uppercase text-sm mb-2 block">{{ CATEGORY_NAME }}</span>
            <h1 class="text-4xl md:text-5xl font-extrabold text-white mb-4">{{ PAGE_TITLE }}</h1>
            <p class="text-slate-300 text-lg max-w-2xl mx-auto">Explore information regarding {{ PAGE_TITLE }} and discover how GARI brings value to the global research community.</p>
        </div>
    </div>
    <div class="py-16 bg-slate-50 min-h-[50vh]">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="bg-white rounded-2xl border border-slate-100 shadow-xl p-8 md:p-12 relative overflow-hidden">
                <div class="prose prose-slate max-w-none text-center">
                    <div class="w-20 h-20 mx-auto rounded-full bg-gradient-to-br from-brand-100 to-indigo-100 flex items-center justify-center text-brand-600 mb-6">
                        <i class="fas fa-tools text-3xl"></i>
                    </div>
                    <h2 class="text-2xl font-bold text-slate-800 mb-4">{{ PAGE_TITLE }} Section</h2>
                    <p class="text-lg text-slate-600 leading-relaxed mb-6 max-w-2xl mx-auto">
                        This section is under active development. Our academic committee is curating the content for <strong>{{ PAGE_TITLE }}</strong>. Please check back later.
                    </p>
                    <a href="{{ url('/') }}" class="inline-flex items-center gap-2 bg-brand-600 text-white px-6 py-3 rounded-full font-bold hover:bg-brand-700 transition shadow-lg shadow-brand-200">
                        <i class="fas fa-home"></i> Return to Homepage
                    </a>
                </div>
            </div>
        </div>
    </div>
</x-main-layout>
"""

for category, page, text in created_routes:
    dir_path = os.path.join(base_dir, 'main', category)
    os.makedirs(dir_path, exist_ok=True)
    file_path = os.path.join(dir_path, f"{page}.blade.php")
    page_content = template_str.replace("{{ PAGE_TITLE }}", text).replace("{{ CATEGORY_NAME }}", category.upper())
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(page_content)

print(f"Generated {len(created_routes)} pages and updated layout!")
