# backend/tests/test_risks.py — 리스크 정확성 (phase-16+: 작업 시드 제거 → 테스트가 직접 생성)
from datetime import date

from tests._factories import make_task


def _seed_risk_tasks(s):
    # 지연 위험: 마감(6/8) 지난 진행중 최상위 작업
    make_task(s, id="k1", title="분기 리포트 초안 마무리", project_id="biz-report",
              status="doing", due=date(2026, 6, 8), assignee_id="jiwoo")
    # 의존성: blocker/blocked 한 쌍(둘 다 미완) — risk.DEPS 의 제목과 일치
    make_task(s, id="dep-a", title="예산 섹션 작성", project_id="biz-report", status="todo")
    make_task(s, id="dep-b", title="경영진 검토 요청 메일", project_id="biz-report",
              status="todo", due=date(2026, 6, 10))


def test_risk_delay_top1(client, session):
    s, _ = session
    _seed_risk_tasks(s)
    r = client.get("/api/risks?area=work").json()
    delay = [x for x in r if x["kind"] == "지연 위험"]
    assert len(delay) == 1
    assert delay[0]["tone"] == "coral" and delay[0]["icon"] == "clock"
    assert delay[0]["task_id"] == "k1"
    assert "6/8" in delay[0]["text"] and "진행 중" in delay[0]["text"]
    assert delay[0]["cta"] == "작업 열기"


def test_risk_dependency(client, session):
    s, _ = session
    _seed_risk_tasks(s)
    r = client.get("/api/risks?area=work").json()
    dep = [x for x in r if x["kind"] == "의존성"]
    assert len(dep) == 1 and dep[0]["tone"] == "violet"
    assert "예산 섹션 작성" in dep[0]["text"] and "경영진 검토 요청 메일" in dep[0]["text"]
    assert dep[0]["cta"] == "후속 작업 보기"


def test_risk_overload(client, session):
    s, _ = session
    _seed_risk_tasks(s)
    r = client.get("/api/risks?area=work").json()
    over = [x for x in r if x["kind"] == "업무 쏠림"]
    assert len(over) <= 1
    if over:
        assert over[0]["tone"] == "amber" and over[0]["task_id"] is None


def test_risks_max_three(client, session):
    s, _ = session
    _seed_risk_tasks(s)
    assert len(client.get("/api/risks?area=work").json()) <= 3


def test_risks_life_scope_ok(client):
    r = client.get("/api/risks?area=life")
    assert r.status_code == 200
