"""Pexels Video API로 background loop 영상 받아오기 (video_loop 모드용)."""
import os
import logging
import httpx

from . import storage

logger = logging.getLogger("music-lab.background")
TIMEOUT_S = 60


async def fetch_video_loop(pipeline_id: int, keyword: str) -> dict:
    """Pexels Video API → 720p HD mp4 다운로드 → /app/data/videos/{id}/loop.mp4 저장.

    반환: {"path": str | None, "used_fallback": bool, "error": str | None}
    """
    api_key = os.getenv("PEXELS_API_KEY", "")
    if not api_key:
        return {"path": None, "used_fallback": True, "error": "PEXELS_API_KEY 미설정"}

    out_dir = storage.pipeline_dir(pipeline_id)
    out_path = os.path.join(out_dir, "loop.mp4")

    try:
        async with httpx.AsyncClient(timeout=TIMEOUT_S) as client:
            resp = await client.get(
                "https://api.pexels.com/videos/search",
                headers={"Authorization": api_key},
                params={"query": keyword or "ambient calm", "per_page": 5,
                        "orientation": "landscape"},
            )
            resp.raise_for_status()
            data = resp.json()
            videos = data.get("videos", [])
            if not videos:
                return {"path": None, "used_fallback": True,
                        "error": f"Pexels 결과 없음: {keyword}"}

            # 720p/1080p HD 우선, 없으면 첫 번째 video file
            chosen = None
            for v in videos:
                for f in v.get("video_files", []):
                    if f.get("quality") == "hd" and f.get("width") in (1280, 1920):
                        chosen = f
                        break
                if chosen:
                    break
            if not chosen:
                chosen = videos[0]["video_files"][0]

            video_url = chosen["link"]
            vid_resp = await client.get(video_url)
            vid_resp.raise_for_status()
            with open(out_path, "wb") as f:
                f.write(vid_resp.content)

            return {"path": out_path, "used_fallback": False, "error": None}
    except (httpx.HTTPError, httpx.TimeoutException, KeyError, ValueError, OSError) as e:
        logger.warning("Pexels video fetch 실패: %s", e)
        return {"path": None, "used_fallback": True, "error": str(e)}
