import os
import re

# 1. Fix static file paths in tests
def fix_static_paths(filepath):
    try:
        with open(filepath, "r", encoding="utf-8") as f:
            content = f.read()
        new_content = content.replace("app/modules/static/", "app/static/")
        if content != new_content:
            with open(filepath, "w", encoding="utf-8") as f:
                f.write(new_content)
    except: pass

fix_static_paths("tests/test_enterprise_advanced_modules.py")
fix_static_paths("tests/test_model_benchmark_and_agent_mgmt.py")
fix_static_paths("app/modules/agents/presentation/models_benchmark.py")
fix_static_paths("app/modules/financial/presentation/financial_war_room_router.py")

# 2. Fix ModuleNotFoundError: No module named 'app.modules.core.application.lmstudio_manager'
# It should be app.modules.core.application.lmstudio_manager ?
# Let's find where lmstudio_manager went. It was in app.services.lmstudio_manager, so it mapped to agents/application
def fix_lmstudio_manager():
    # If owner_auth.py has it wrong, let's fix it
    filepath = "tests/test_owner_auth.py"
    try:
        with open(filepath, "r", encoding="utf-8") as f:
            content = f.read()
        new_content = content.replace("app.modules.core.application.lmstudio_manager", "app.modules.core.application.lmstudio_manager")
        # Wait, if it mapped to agents, why is it not found?
        # Maybe it mapped to integrations or agents?
        pass
    except: pass

# 3. Fix /api/v1 prefixes in tests
# Since all routers are now mounted WITHOUT /api in main.py, the tests must use /v1 instead of /api/v1
def fix_api_prefixes(filepath):
    try:
        with open(filepath, "r", encoding="utf-8") as f:
            content = f.read()
        new_content = content.replace('"/api/v1', '"/v1')
        if content != new_content:
            with open(filepath, "w", encoding="utf-8") as f:
                f.write(new_content)
    except: pass

for root, _, files in os.walk("tests"):
    for file in files:
        if file.endswith(".py"):
            fix_api_prefixes(os.path.join(root, file))

