"""Shared file proxy: relay uploads from the FastAPI server to the browser.

The browser can't send the X-API-Key header directly, so blueprints expose
a proxy route and delegate here.
"""
import requests as http_requests
from flask import Response, session

from config import Config


def proxy_file(file_path: str) -> Response:
    """Fetch a file from the API server and relay it with its content type."""
    base_url = Config.API_SERVER_URL.rstrip("/")
    resp = http_requests.get(
        f"{base_url}/api/files/{file_path}",
        headers={"X-API-Key": session.get("api_key", "")},
        timeout=30,
    )
    if resp.status_code != 200:
        return Response(resp.text, status=resp.status_code)
    return Response(
        resp.content,
        content_type=resp.headers.get("content-type", "application/octet-stream"),
    )
