from app.interpret.schema import validate_saju_interpretation, validate_compat_interpretation


def _valid_saju_item(key="기질"):
    return {
        "key": key, "title": "타고난 기질",
        "content": "본문 3~5문장",
        "evidence": {"saju_element": "갑목 일주", "reasoning": "..."}
    }


SAJU_ITEM_KEYS = [
    "기질", "오행밸런스", "지지상호작용", "신살영향",
    "재물운", "직업적성", "애정운", "건강운",
    "현재대운", "올해세운", "인생황금기", "종합조언",
]


def _valid_saju_payload():
    return {
        "items": [_valid_saju_item(k) for k in SAJU_ITEM_KEYS],
        "summary": "...",
        "advice": "...",
        "warning": None,
        "confidence": "medium",
    }


def test_valid_saju():
    ok, _ = validate_saju_interpretation(_valid_saju_payload())
    assert ok is True


def test_saju_missing_items():
    p = _valid_saju_payload(); del p["items"]
    ok, err = validate_saju_interpretation(p)
    assert not ok and "items" in err


def test_saju_wrong_item_count():
    p = _valid_saju_payload(); p["items"] = p["items"][:5]
    ok, err = validate_saju_interpretation(p)
    assert not ok and ("12" in err or "items" in err)


def test_saju_missing_evidence():
    p = _valid_saju_payload(); del p["items"][0]["evidence"]
    ok, err = validate_saju_interpretation(p)
    assert not ok and "evidence" in err


def test_saju_invalid_confidence():
    p = _valid_saju_payload(); p["confidence"] = "extreme"
    ok, err = validate_saju_interpretation(p)
    assert not ok and "confidence" in err


def _valid_compat_payload():
    return {
        "summary": "두 사주 궁합 핵심",
        "strengths": [
            {"title": "오행 상생", "explanation": "...", "evidence": "甲木 → 丁火"},
            {"title": "일지 합", "explanation": "...", "evidence": "申/巳 합"},
        ],
        "challenges": [
            {"title": "...", "explanation": "...", "evidence": "..."},
            {"title": "...", "explanation": "...", "evidence": "..."},
        ],
        "advice": "...",
        "warning": None,
        "confidence": "high",
    }


def test_valid_compat():
    ok, _ = validate_compat_interpretation(_valid_compat_payload())
    assert ok is True


def test_compat_missing_strengths():
    p = _valid_compat_payload(); del p["strengths"]
    ok, err = validate_compat_interpretation(p)
    assert not ok and "strengths" in err


def test_compat_invalid_confidence():
    p = _valid_compat_payload(); p["confidence"] = "absolute"
    ok, err = validate_compat_interpretation(p)
    assert not ok and "confidence" in err
