import httpx from dotenv import load_dotenv from fastapi import APIRouter, Query from fastapi.responses import JSONResponse from services.tmdb import TMDBNotConfiguredError, search_movies load_dotenv() router = APIRouter(prefix="/tmdb", tags=["tmdb"]) @router.get("/search") async def search_tmdb(q: str = Query(..., min_length=2)): try: return {"results": await search_movies(q, limit=8, include_details=True)} except TMDBNotConfiguredError: return JSONResponse( status_code=503, content={ "error": "TMDB_API_KEY is not configured.", "results": [], }, ) except httpx.HTTPError: return JSONResponse( status_code=502, content={ "error": "TMDB search failed. Check your API key and try again.", "results": [], }, )