import os
import re

def fix_urls(filepath, domain):
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Replace href="/..." with href="domain/..."
    content = re.sub(r'href="/([^/])', rf'href="{domain}/\1', content)
    # Replace src="/..." with src="domain/..."
    content = re.sub(r'src="/([^/])', rf'src="{domain}/\1', content)
    # Also handle CSS cases like url(/resources/...)
    content = re.sub(r'url\s*\(\s*[\'"]?/([^\'\")]+)[\'"]?\s*\)', rf'url("{domain}/\1")', content)

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

base_dir = r'checkout'
fix_urls(os.path.join(base_dir, 'selection', 'index.html'), 'https://tbpg.checkout.tuboleta.com')
fix_urls(os.path.join(base_dir, 'cart', 'index.html'), 'https://tbpg.checkout.tuboleta.com')

auth_path = os.path.join(base_dir, 'auth', 'index.html')
if os.path.exists(auth_path):
    fix_urls(auth_path, 'https://all.checkout.tuboleta.com')

print("Relative URLs fixed.")
