import json

from src.video_tts_classifier import (
    OpenRouterVideoTtsClassifier,
    normalize_metadata_row,
    derive_heuristics,
)


def _sample_row(video_id: str = "abc123") -> dict[str, str]:
    return {
        "video_id": video_id,
        "channel_id": "chan1",
        "channel_title": "StudyGuide Hub",
        "title": "Psychology Podcast Episode 5",
        "description": "A long-form educational discussion about happiness and behavior.",
        "tags": json.dumps(["podcast", "psychology", "education"]),
        "category_id": "22",
        "default_language": "en",
        "default_audio_language": "en",
        "duration": "PT6M45S",
        "duration_seconds": "405",
        "definition": "hd",
        "topic_categories": json.dumps(["https://en.wikipedia.org/wiki/Education"]),
        "fetch_status": "ok",
    }


def test_normalize_and_heuristics_detect_podcast_signals():
    metadata = normalize_metadata_row(_sample_row())
    heuristics = derive_heuristics(metadata)

    assert metadata["category_label"] == "People & Blogs"
    assert heuristics["podcast_like"] is True
    assert "podcast" in heuristics["positive_title_hits"]
    assert heuristics["duration_minutes"] == 6.75


def test_request_body_uses_cached_static_prefix():
    classifier = OpenRouterVideoTtsClassifier("fake-key")
    body = classifier.build_request_body([_sample_row()])

    assert body["model"] == "google/gemini-3.1-pro-preview"
    assert body["provider"]["require_parameters"] is True
    assert body["reasoning"]["effort"] == "low"
    assert body["messages"][1]["content"][1]["cache_control"]["type"] == "ephemeral"
    assert "video_metadata" in body["messages"][2]["content"][0]["text"]


def test_parse_response_json_handles_code_fences():
    content = """```json
{
  "recommended_action": "keep",
  "likely_content_type": "podcast",
  "tts_suitability_score": 85,
  "spoken_word_score": 95,
  "clean_speech_likelihood_score": 85,
  "single_speaker_likelihood_score": 70,
  "metadata_confidence_score": 90,
  "hard_reject": false,
  "hard_reject_reasons": [],
  "positive_signals": ["clear_podcast_or_interview_signal"],
  "risk_signals": ["panel_or_multi_speaker"],
  "short_rationale": "Clear podcast metadata with strong spoken-word cues.",
  "needs_audio_validation": true
}
```"""
    parsed = OpenRouterVideoTtsClassifier.parse_response_json(content)
    assert parsed["recommended_action"] == "keep"
    assert parsed["likely_content_type"] == "podcast"
