from pathlib import Path

from fastapi.responses import FileResponse, Response

from app.core.config import get_settings
from app.utils.file_storage import get_file_storage, read_storage_bytes


def pdf_file_response(storage_path: str, filename: str) -> FileResponse | Response:
    settings = get_settings()
    storage = get_file_storage(settings)
    local = storage.local_path(storage_path)
    if local is not None:
        return FileResponse(local, media_type="application/pdf", filename=filename)
    data = read_storage_bytes(storage_path, settings)
    headers = {"Content-Disposition": f'inline; filename="{filename}"'}
    return Response(content=data, media_type="application/pdf", headers=headers)
