
import os
import re

BASE_DIR = r"d:\PROGRAMMING\HoanhTaoVoLam\web\hoanhtaovolam"
CAMNANG_DIR = os.path.join(BASE_DIR, "cam-nang")
CAMNANG_FILE = os.path.join(BASE_DIR, "cam-nang.html")

# Replacements (Legacy -> New Absolute Path)
REPLACEMENTS = {
    # Homepage
    'index-2.html': '/',
    'index.html': '/',
    'index.php': '/',
    '../../index-2.html': '/',
    '../../index.html': '/',
    '../../index.php': '/',
    '../../../index-2.html': '/',
    '../../../index.php': '/',
    
    # News
    'tin-tuc.html': '/tin-tuc/',
    '../../tin-tuc.html': '/tin-tuc/',
    
    # Download
    'download.html': '/download/',
    '../../download.html': '/download/',
    
    # Auth (Old domains)
    'https://id.hoanhtaogiangho.com/client/sign-up': '/accounts/register/',
    'https://id.hoanhtaogiangho.com/client/sign-in': '/accounts/login/',
    'https://hoanhtaovolam.com/UCClient/register.php': '/accounts/register/',
    'https://hoanhtaovolam.com/UCClient/login.php': '/accounts/login/',
    'http://hoanhtaovolam.com/UCClient/register.php': '/accounts/register/',
    'http://hoanhtaovolam.com/UCClient/login.php': '/accounts/login/',
    
    # Auth (Relative)
    '../../UCClient/register.php': '/accounts/register/',
    '../../UCClient/login.php': '/accounts/login/',
    '/UCClient/register.php': '/accounts/register/',
    '/UCClient/login.php': '/accounts/login/',
    
    # Cam Nang Root
    'cam-nang.html': '/cam-nang/',
    '../../cam-nang.html': '/cam-nang/',
    
    # Cleanup hashes
    '#https://www.facebook.com/HoanhTao3D': 'https://www.facebook.com/61573366070979/',
    'https://www.facebook.com/HoanhTao3D': 'https://www.facebook.com/61573366070979/',
}

def update_content(content):
    # Apply direct string replacements for hrefs
    for old, new in REPLACEMENTS.items():
        # Replace href="old" with href="new"
        pattern = f'href="{re.escape(old)}"'
        replacement = f'href="{new}"'
        content = re.sub(pattern, replacement, content)
        
        # Replace href='old' with href='new'
        pattern_qt = f"href='{re.escape(old)}'"
        replacement_qt = f"href='{new}'"
        content = re.sub(pattern_qt, replacement_qt, content)

    # Special handling for cam-nang sub-links in cam-nang.html
    # href="cam-nang/51/..." -> href="/cam-nang/51/..."
    content = re.sub(r'href="cam-nang/(\d+)/', r'href="/cam-nang/\1/', content)
    
    return content

def process_file(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
            
        new_content = update_content(content)
        
        if content != new_content:
            print(f"Updating {file_path}...")
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(new_content)
    except Exception as e:
        print(f"Error processing {file_path}: {e}")

def main():
    # Process main cam-nang.html
    if os.path.exists(CAMNANG_FILE):
        process_file(CAMNANG_FILE)
    
    # Process all files in cam-nang directory
    for root, dirs, files in os.walk(CAMNANG_DIR):
        for file in files:
            if file.endswith('.html') or file.endswith('.htm'):
                process_file(os.path.join(root, file))

if __name__ == "__main__":
    main()
