"""
FastAPI Implementation for proxy redirection.
Maps FastAPI Request objects to the pure proxy logic and returns a FastAPI Response.
"""

from fastapi import Request, Response

from proxy_redirect import ProxyRedirectBase


class FastAPIProxyRedirect(ProxyRedirectBase):
    """
    Subclass of ProxyRedirectBase that interfaces with FastAPI objects.
    """

    def __init__(self, request: Request):
        """
        Transforms the FastAPI Request headers into an environment dictionary
        compatible with ProxyRedirectBase.
        """
        env_data = {
            "BACKEND_SERVER": request.headers.get("X-Backend-Server", ""),
            "AUTH_REDIRECT_LOCATION": request.headers.get("X-Auth-Redirect", ""),
            "APP_REDIRECT_LOCATION": request.headers.get("X-App-Redirect", ""),
            "AUTH_STATUS": request.headers.get("X-Auth-Status", ""),
            "REDIRECT_STATUS": request.headers.get("X-Redirect-Status", "302"),
            "REQUEST_URI": str(request.url),
            "REQUEST_METHOD": request.method,
        }
        super().__init__(env_data)

    def get_response(self) -> Response:
        """
        Executes the pure logic and returns a FastAPI-compatible Response.
        """
        data = self.run()

        return Response(
            content=data.get("body", ""),
            status_code=data.get("status", 200),
            headers=data.get("headers", {}),
        )
