import re

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

# Make sure aimodel_selector can be imported correctly from parent dir
import_sys = "import sys\nimport os\nsys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))\nfrom skilldarbar_api.aimodel_selector.models_config import get_model_config, get_default_model, get_all_models\n"
content = re.sub(r'from aimodel_selector\.models_config import get_model_config, get_default_model, get_all_models\n', import_sys, content)

# update `/models` route
new_models = '''
@ai_bp.route('/models', methods=['GET'])
@token_required
def get_ai_models(current_user, user_role):
    """Get available AI models (alias for existing route but scoped to /api/ai)"""
    try:
        models = get_all_models()
        return jsonify({
            'status': 'success',
            'models': models
        }), 200
    except Exception as e:
        return create_error_response(f"Error fetching models: {str(e)}", 500)
'''
content = re.sub(r"@ai_bp\.route\('/models', methods=\['GET'\]\).*?def get_ai_models\(current_user, user_role\):.*?\]\}\), 200\n    except Exception as e:\n        return create_error_response\(f\"Error fetching models: \{str\(e\)\}\", 500\)", new_models, content, flags=re.DOTALL)

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

print("Updated ai_routes.py imports")
