import os
import glob
import re

base_file = 'train-booking.html'
with open(base_file, 'r') as f:
    base_html = f.read()

brands = [
    ('Greyhound Lines', 'greyhound-bus.html', 'Greyhound'),
    ('FlixBus', 'flixbus.html', 'FlixBus'),
    ('Megabus', 'megabus.html', 'Megabus'),
    ('Trailways', 'trailways.html', 'Trailways')
]

# Create the dropdown HTML
dropdown_html = """                    <li class="dropdown">
                        <a href="#">Bus Booking <i class="fa-solid fa-chevron-down"></i></a>
                        <ul class="dropdown-menu">
                            <li><a href="greyhound-bus.html">Greyhound Lines</a></li>
                            <li><a href="flixbus.html">FlixBus</a></li>
                            <li><a href="megabus.html">Megabus</a></li>
                            <li><a href="trailways.html">Trailways</a></li>
                        </ul>
                    </li>"""

nav_search = r'<li><a href="train-booking.html"( class="active")?>Train Booking</a></li>'
nav_replace = dropdown_html + '\n                    ' + r'<li><a href="train-booking.html"\1>Train Booking</a></li>'

def process_file(filepath):
    with open(filepath, 'r') as f:
        content = f.read()
    
    if 'Bus Booking' not in content:
        content = re.sub(nav_search, nav_replace, content)
        with open(filepath, 'w') as f:
            f.write(content)

# Update all existing HTML files
for file in glob.glob('*.html'):
    process_file(file)

# Generate new brand files
for brand_name, brand_file, short_name in brands:
    content = base_html
    # Replace navigation to make the bus link active if needed, but for now we just use the updated base HTML
    
    content = re.sub(r'<title>.*?</title>', f'<title>{brand_name} Tickets - Travel Touristo</title>', content)
    content = content.replace('Train Bookings', f'{brand_name} Tickets')
    content = content.replace('train routes across the country', f'bus routes provided by {brand_name}')
    content = content.replace('Train Booking - Travel Touristo', f'{brand_name} Booking - Travel Touristo')
    content = content.replace('fa-train-subway', 'fa-bus')
    content = content.replace('fa-train-tram', 'fa-bus-simple')
    content = content.replace('fa-train', 'fa-bus')
    content = content.replace('Train Search Widget', 'Bus Search Widget')
    content = content.replace('Find Trains', 'Find Buses')
    content = content.replace('Popular Train Routes', f'Popular {short_name} Routes')
    content = content.replace('scenic and efficient train journeys', f'convenient and affordable bus journeys with {brand_name}')
    content = content.replace('Why Choose Train Travel?', f'Why Book {short_name} Tickets?')
    content = content.replace('Traveling by train offers', 'Traveling by bus offers')
    content = content.replace('our train routes', f'our {short_name} bookings')
    content = content.replace('Train travel is one of the most', 'Bus travel is one of the most')
    
    # Disclaimer
    disclaimer_search = r'<strong>Disclaimer:</strong>.*?</p>'
    disclaimer_replace = f'<strong>Disclaimer:</strong> Travel Touristo is an independent travel and bus booking comparison portal. We are not officially affiliated with, endorsed by, or partnered with {brand_name}, Greyhound, FlixBus, Megabus, Trailways or any specific bus brand mentioned on this site. All trademarks belong to their respective owners.</p>'
    content = re.sub(disclaimer_search, disclaimer_replace, content)

    # Ensure Bus Booking nav is added if not there
    if 'Bus Booking' not in content:
        content = re.sub(nav_search, nav_replace, content)
        
    with open(brand_file, 'w') as f:
        f.write(content)

print("Done generating files.")
