import os
import glob
import re

html_files = glob.glob('*.html')

# 1. Create header.php
with open('index.html', 'r') as f:
    index_content = f.read()

# Extract header
header_match = re.search(r'(<!DOCTYPE html>.*?</header>)', index_content, re.DOTALL)
if header_match:
    header_content = header_match.group(1)
    # Replace title
    header_content = re.sub(r'<title>.*?</title>', r'<title><?php echo isset($page_title) ? $page_title : "Travel Touristo"; ?></title>', header_content)
    # Change .html links to .php in header
    header_content = header_content.replace('.html', '.php')
    # Remove existing active classes to prevent hardcoded active state
    header_content = header_content.replace('class="active"', '')
    # Add dynamic active class logic
    # Find all nav links: <li><a href="something.php">
    def add_active_logic(match):
        href = match.group(1)
        text = match.group(2)
        # Only add to main links, ignore #
        if href == '#': return match.group(0)
        return f'<a href="{href}" class="<?php echo (basename($_SERVER[\'PHP_SELF\']) == \'{href}\') ? \'active\' : \'\'; ?>">{text}</a>'
    
    header_content = re.sub(r'<a href="([^"]+)">([^<]+)</a>', add_active_logic, header_content)

    with open('header.php', 'w') as f:
        f.write(header_content)

# Extract footer
footer_match = re.search(r'(<!-- Footer -->.*</html>)', index_content, re.DOTALL)
if footer_match:
    footer_content = footer_match.group(1)
    
    # Replace disclaimer with dynamic PHP variable
    disclaimer_pattern = r'<div class="footer-disclaimer">\s*(.*?)\s*</div>'
    default_disclaimer = re.search(disclaimer_pattern, footer_content, re.DOTALL).group(1)
    
    dynamic_disclaimer = f"""<div class="footer-disclaimer">
                <?php if(isset($footer_disclaimer)): ?>
                    <?php echo $footer_disclaimer; ?>
                <?php else: ?>
                    {default_disclaimer}
                <?php endif; ?>
            </div>"""
    
    footer_content = re.sub(disclaimer_pattern, dynamic_disclaimer, footer_content, flags=re.DOTALL)
    # Change .html to .php in footer
    footer_content = footer_content.replace('.html', '.php')
    
    with open('footer.php', 'w') as f:
        f.write(footer_content)

# Process all HTML files
for file in html_files:
    with open(file, 'r') as f:
        content = f.read()
    
    # Extract title
    title_match = re.search(r'<title>(.*?)</title>', content)
    page_title = title_match.group(1) if title_match else "Travel Touristo"
    # Escape quotes
    page_title = page_title.replace("'", "\\'")

    # Extract disclaimer
    disclaimer_match = re.search(r'<div class="footer-disclaimer">\s*(.*?)\s*</div>', content, re.DOTALL)
    footer_disclaimer = disclaimer_match.group(1) if disclaimer_match else ""
    # Escape single quotes in disclaimer
    footer_disclaimer = footer_disclaimer.replace("'", "\\'")

    # Extract body content (between header and footer)
    body_match = re.search(r'</header>(.*?)<!-- Footer -->', content, re.DOTALL)
    if body_match:
        body_content = body_match.group(1).strip()
        # Change .html to .php in body content
        body_content = body_content.replace('.html', '.php')
        
        php_filename = file.replace('.html', '.php')
        
        with open(php_filename, 'w') as f:
            f.write(f"""<?php
$page_title = '{page_title}';
$footer_disclaimer = '{footer_disclaimer}';
include 'header.php';
?>

{body_content}

<?php include 'footer.php'; ?>
""")

# Delete all HTML files
for file in html_files:
    os.remove(file)

print("Conversion complete.")
