# backend/tests/test_multimodal.py — STT/Vision 폴백 골든 + 멀티모달→분류 + 주간 리뷰
import json

from app.multimodal.factory import get_stt, get_vision


def test_stt_heuristic_golden():
    t = get_stt("heuristic").transcribe(b"", hint="pool")
    assert t.text == "음성 메모 0:14 — 수영장 차광막 부품 알아보기"
    assert t.model == "heuristic"


def test_vision_heuristic_golden():
    c = get_vision("heuristic").describe(b"", hint="clip")
    assert c.text == "캡처 사진 — 차광막 클립 부품"
    assert c.model == "heuristic"


def test_stt_fallback_when_no_hint():
    t = get_stt("heuristic").transcribe(b"", hint="")
    assert "텍스트로 적어주세요" in t.text  # phase-4 폴백 톤


def test_transcribe_endpoint(client):
    r = client.post(
        "/api/inbox/transcribe",
        files={"audio": ("a.webm", b"", "audio/webm")},
        data={"hint": "pool"},
    )
    assert r.status_code == 200, r.text
    body = r.json()
    assert body["text"] == "음성 메모 0:14 — 수영장 차광막 부품 알아보기"
    assert body["model"] == "heuristic"


def test_caption_endpoint(client):
    r = client.post(
        "/api/inbox/caption",
        files={"image": ("c.jpg", b"", "image/jpeg")},
        data={"hint": "clip"},
    )
    assert r.status_code == 200, r.text
    assert r.json()["text"] == "캡처 사진 — 차광막 클립 부품"


def test_transcribe_then_capture_classifies(client):
    text = client.post(
        "/api/inbox/transcribe",
        files={"audio": ("a.webm", b"", "audio/webm")},
        data={"hint": "pool"},
    ).json()["text"]
    cap = client.post("/api/inbox/capture", json={"kind": "voice", "raw": text}).json()
    # phase-4 분류 파이프라인이 그대로 동작(raw 가 실제로 채워짐)
    assert cap["item"]["kind"] == "voice"
    assert cap["classification"]["type"] in ("idea", "task", "event", "note", "reminder")


def test_weekly_review_endpoint(client):
    wr = client.get("/api/weekly-review?week=6/7~6/13").json()
    assert wr["deep_work_h"] == 8.0 and wr["meeting_h"] == 12.0
    assert any(p["action_kind"] == "automation" for p in wr["patterns"])
    assert "보호할까요" in (wr["note"] or "")


def test_weekly_review_computes_new_week(session):
    s, _ = session
    from app.worker.jobs.weekly_review import run_weekly_review

    wr = run_weekly_review(s, week_label="6/14~6/20")
    assert wr.deep_work_h > 0 and wr.meeting_h > 0
    patterns = json.loads(wr.patterns_json)
    assert any(p["action_kind"] == "automation" for p in patterns)
