import re

with open('routes/ai_routes.py', 'r') as f:
    content = f.read()

# Add import
import_stmt = "from aimodel_selector.models_config import get_model_config, get_default_model, get_all_models\n"
content = re.sub(r'import g4f\n', f'import g4f\n{import_stmt}', content)

# Replace get_default_model_config()
new_get_default_model_config = '''def get_default_model_config():
    """Get the active AI model configuration from aimodel_selector."""
    try:
        default_model_name = get_default_model()
        model_config = get_model_config(default_model_name)
        return {
            'provider': model_config.get('provider', 'PollinationsAI'),
            'model': model_config.get('model', 'geminisearch')
        }
    except Exception as e:
        logger.error(f"Error fetching model config: {str(e)}")
        return {
            'provider': 'AnyProvider',
            'model': 'gpt-4'
        }
'''
content = re.sub(r'def get_default_model_config\(\):(.*?)(?=\n@ai_bp\.route)', new_get_default_model_config, content, flags=re.DOTALL)

with open('routes/ai_routes.py', 'w') as f:
    f.write(content)

print("Updated ai_routes.py")
