def test_review_required_risk_level():
    from app.ai.pose_metrics import LEFT_HIP, LEFT_SHOULDER, NOSE, RIGHT_HIP, RIGHT_SHOULDER, build_screening_report

    bad_frame = {
        NOSE: {"x": 0.85, "y": 0.28, "visibility": 0.95},
        LEFT_SHOULDER: {"x": 0.42, "y": 0.35, "visibility": 0.95},
        RIGHT_SHOULDER: {"x": 0.58, "y": 0.48, "visibility": 0.95},
        LEFT_HIP: {"x": 0.44, "y": 0.62, "visibility": 0.95},
        RIGHT_HIP: {"x": 0.56, "y": 0.50, "visibility": 0.95},
    }
    report = build_screening_report([bad_frame] * 6)
    assert report["riskLevel"] == "review_required"


def test_admin_report_review_flow(client, auth_headers, staff_headers, db_session):
    from app.db.models import Report

    child_id = client.post(
        "/api/children",
        headers=auth_headers,
        json={"name": "复核测试", "birthday": "2016-06-01", "gender": "female"},
    ).json()["data"]["id"]

    object_key = f"videos/{child_id}/review.mp4"
    video_id = client.post(
        "/api/videos",
        headers=auth_headers,
        json={"childId": child_id, "objectKey": object_key, "scene": "front_posture"},
    ).json()["data"]["id"]

    task = client.post(
        "/api/analysis/tasks",
        headers={**auth_headers, "Idempotency-Key": "sprint6-review"},
        json={"childId": child_id, "videoId": video_id, "taskType": "posture_screening"},
    ).json()["data"]
    report_id = task["reportId"]
    assert report_id

    report = db_session.get(Report, report_id)
    report.status = "pending_review"
    report.risk_level = "high"
    db_session.commit()

    pending = client.get("/api/admin/reports/pending", headers=staff_headers)
    assert pending.status_code == 200
    assert any(item["id"] == report_id for item in pending.json()["data"]["list"])

    approved = client.post(
        f"/api/admin/reports/{report_id}/review",
        headers=staff_headers,
        json={"action": "approve", "note": "指标可接受，发布报告"},
    )
    assert approved.status_code == 200
    assert approved.json()["data"]["status"] == "published"
    assert approved.json()["data"]["reviewedBy"]

    detail = client.get(f"/api/reports/{report_id}", headers=auth_headers)
    assert detail.json()["data"]["status"] == "published"


def test_health_sprint6(client):
    resp = client.get("/health")
    assert resp.json()["stage"] == "sprint6"
