# --- START OF FILE middlewares.py ---
    
from aiohttp import web
    
@web.middleware
async def admin_auth_middleware(request, handler):
    public_paths = [
        '/api/agents', 
        '/api/session',
        '/api/sessions',
    ]

    # Для публичных GET-запросов пропускаем проверку
    if request.method == 'GET' and request.path in public_paths:
        return await handler(request)

    # Разрешаем публичные POST-запросы
    if request.method == 'POST':
        if request.path.endswith('/transcript') or \
           request.path.endswith('/recordings') or \
           request.path == '/api/realtime': # <-- ИЗМЕНЕНИЕ ЗДЕСЬ
            return await handler(request)

    # Для всех остальных запросов к /api/ (админских) проверяем ключ
    if request.path.startswith('/api/'):
        admin_key = request.app.get("ADMIN_API_KEY")
        if request.headers.get("X-Admin-API-Key") != admin_key:
            return web.json_response({"error": "Unauthorized. 'X-Admin-API-Key' header is missing or invalid."}, status=403)
    
    return await handler(request)

# --- END OF FILE middlewares.py ---