"""SP-6 — Windows music-render → NAS internal webhook.

POST /api/internal/music/update
- X-Internal-Key 인증 필수
- music_tasks 테이블 row update (status, progress, message, audio_url, error)
- 옵션 `track` 페이로드가 있으면 music_library에 add_track 호출
"""
from __future__ import annotations

import logging
from typing import Any, Dict, Optional

from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field

from . import db
from .auth import verify_internal_key

logger = logging.getLogger(__name__)
router = APIRouter()


class UpdatePayload(BaseModel):
    task_id: str
    status: str = Field(..., description="processing|succeeded|failed")
    progress: int = Field(..., ge=0, le=100)
    message: str = ""
    audio_url: Optional[str] = None
    error: Optional[str] = None
    track: Optional[Dict[str, Any]] = None


@router.post(
    "/api/internal/music/update",
    dependencies=[Depends(verify_internal_key)],
)
def music_update(payload: UpdatePayload):
    task = db.get_task(payload.task_id)
    if task is None:
        raise HTTPException(404, f"task not found: {payload.task_id}")

    db.update_task(
        payload.task_id,
        payload.status,
        payload.progress,
        message=payload.message,
        audio_url=payload.audio_url,
        error=payload.error,
    )

    if payload.track is not None:
        try:
            db.add_track(payload.track)
        except Exception:
            logger.exception("add_track 실패 task=%s (무시)", payload.task_id)

    logger.info(
        "internal/music/update task=%s status=%s progress=%d",
        payload.task_id, payload.status, payload.progress,
    )
    return {"ok": True}
