import os
import re

api_import = "import { API_BASE_URL } from '../../lib/config';"
# Need to adjust depth based on file location
# But I can just use a relative path that works for most components

def fix_file(path):
    with open(path, 'r') as f:
        content = f.read()
    
    # Calculate depth for import
    rel_path = os.path.relpath('src/lib/config.ts', os.path.dirname(path))
    rel_path = rel_path.replace('.ts', '').replace('\\', '/')
    if not rel_path.startswith('.'):
        rel_path = './' + rel_path
    
    import_line = f"import {{ API_BASE_URL }} from '{rel_path}';"
    
    # Replace the messy strings
    new_content = content.replace("'http://' + window.location.hostname + ':8080", "API_BASE_URL")
    new_content = new_content.replace("`http://' + window.location.hostname + ':8080", "`${API_BASE_URL}")
    
    if new_content != content:
        # Add import if not present
        if 'API_BASE_URL' in new_content and 'import { API_BASE_URL }' not in new_content:
            new_content = import_line + "\n" + new_content
        
        with open(path, 'w') as f:
            f.write(new_content)
        print(f"Fixed {path}")

for root, dirs, files in os.walk('src'):
    for file in files:
        if file.endswith(('.ts', '.tsx')):
            fix_file(os.path.join(root, file))
