"""
Middleware para agregar headers de no-cache a las respuestas
"""
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response


class NoCacheMiddleware(BaseHTTPMiddleware):
    """
    Middleware que agrega headers de no-cache a todas las respuestas
    que sirven archivos de la carpeta public
    """
    
    async def dispatch(self, request: Request, call_next):
        response = await call_next(request)
        
        # Lista de rutas que deben tener headers de no-cache
        no_cache_paths = [
            "/",
            "/register",
            "/verify",
            "/express/",
            "/register/",
            "/images/"
        ]
        
        # Verificar si la ruta actual debe tener headers de no-cache
        should_no_cache = any(
            request.url.path.startswith(path) for path in no_cache_paths
        )
        
        if should_no_cache:
            # Agregar headers de no-cache
            response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, max-age=0"
            response.headers["Pragma"] = "no-cache"
            response.headers["Expires"] = "0"
        
        return response
