import os
import re

directory = '/Applications/XAMPP/xamppfiles/htdocs/aaa'

# 1. First, fix footer.php contact information
footer_path = os.path.join(directory, 'footer.php')
if os.path.exists(footer_path):
    with open(footer_path, 'r', encoding='utf-8') as f:
        footer_content = f.read()

    # Fix JSON LD
    old_address = '"address":{"@type":"PostalAddress","addressLocality":"Bangalore","addressCountry":"IN","addressRegion":"Karnataka","postalCode":"560103","streetAddress":"Vaishnavi Tech Park, 6th Floor,16/1, Ambalipura - Sarjapur Rd, Bellandur"}'
    new_address = '"address":{"@type":"PostalAddress","addressLocality":"New York","addressCountry":"US","addressRegion":"NY","postalCode":"10005","streetAddress":"1 E 2nd St"}'
    footer_content = footer_content.replace(old_address, new_address)
    
    footer_content = footer_content.replace('"email":"infoinfo@surecertificate.online.online"', '"email":"info@surecertificate.online"')
    footer_content = footer_content.replace('"telephone":"080 6547 4648"', '"telephone":"+1 (888) 765-8856"')
    
    # Fix the visual USA block
    old_usa_block = '<div class="flex text-sm leading-5.5 text-neutral-500 flex-col"><a class="mt-spacing4 " href="tel:+1 (888) 765-8856" rel="nofollow">+1 (888) 765-8856</a><a class="mt-spacing4 " href="tel:+1 (888) 765-8856" rel="nofollow">+1 (888) 765-8856</a></div>'
    new_usa_block = '<div class="flex text-sm leading-5.5 text-neutral-500 flex-col"><a class="mt-spacing4 " href="tel:+1 (888) 765-8856" rel="nofollow">+1 (888) 765-8856</a><a class="mt-spacing4" href="mailto:info@surecertificate.online">info@surecertificate.online</a><span class="mt-spacing4">1 E 2nd St, New York, NY 10005</span></div>'
    footer_content = footer_content.replace(old_usa_block, new_usa_block)

    with open(footer_path, 'w', encoding='utf-8') as f:
        f.write(footer_content)
    print("Fixed footer.php contact details")


# 2. Bulk link fixes
def process_file(filepath):
    try:
        with open(filepath, 'r', encoding='utf-8') as f:
            content = f.read()

        changed = False

        # Replace standard href=".html" with ".php"
        # Since 'href="foo.html"' -> 'href="foo.php"'
        new_content = re.sub(r'href="([^"]+)\.html"', r'href="\1.php"', content)
        if new_content != content:
            content = new_content
            changed = True
            
        # Replace broken images 
        new_content = new_content.replace('d2o2utebsixu4k.cloudfront.net', 'images.unsplash.com/photo-1516321318423-f06f85e504b3?ixlib=rb-4.0.3&auto=format&fit=crop&w=800&q=80')
        if new_content != content:
            content = new_content
            changed = True

        if changed:
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(content)
                
    except Exception as e:
        print(f"Error processing {filepath}: {e}")

# Process files
for root, dirs, files in os.walk(directory):
    for filename in files:
        filepath = os.path.join(root, filename)
        
        # We only care about PHP files now since user migrated some, but what if there are HTML files?
        if filename.endswith(('.php', '.html')):
            process_file(filepath)
            
        # Rename .html to .php
        if filename.endswith('.html'):
            new_filepath = os.path.join(root, filename[:-5] + '.php')
            os.rename(filepath, new_filepath)

print("Finished bulk replacing links and renaming HTML to PHP.")
