from autoshorts.audio.voiceover import (
    FinalVoiceoverScript,
    build_accessible_script_draft_from_voiceover,
    build_truetrace_final_voiceover,
    validate_final_voiceover_script,
)


def test_build_truetrace_final_voiceover_for_quiet_rot_is_beginner_friendly_and_concrete():
    script = build_truetrace_final_voiceover("truetrace-ai-automation-quiet-rot-38s")

    assert script.candidate_id == "truetrace-ai-automation-quiet-rot-38s"
    assert script.language == "en"
    assert script.voice == "en-GB-RyanNeural"
    assert script.target_duration_seconds == 37
    assert len(script.text.split()) <= 85
    assert script.text.startswith("Your AI automation works perfectly in the demo.")
    assert "weird email" in script.text
    assert "real life shows up" in script.text
    assert "exception path" not in script.text.casefold()
    assert "input boundary" not in script.text.casefold()
    assert "output standard" not in script.text.casefold()
    assert "Follow" not in script.text
    assert script.side_effects == ()
    assert script.external_calls == ()


def test_build_truetrace_final_voiceover_has_all_top3_scripts_with_stable_duration():
    ids = (
        "truetrace-ai-automation-quiet-rot-38s",
        "truetrace-prompt-vs-system-workflow-breaks-35s",
        "truetrace-ai-notes-equal-importance-34s",
    )

    scripts = tuple(build_truetrace_final_voiceover(candidate_id) for candidate_id in ids)

    assert [script.target_duration_seconds for script in scripts] == [37, 23, 22]
    assert all(45 <= len(script.text.split()) <= 85 for script in scripts)
    assert all(script.text.count(".") >= 3 for script in scripts)


def test_build_accessible_script_draft_from_voiceover_uses_one_text_source_for_visuals():
    voiceover = build_truetrace_final_voiceover("truetrace-ai-automation-quiet-rot-38s")

    draft = build_accessible_script_draft_from_voiceover(voiceover)

    assert draft.brief_id == voiceover.candidate_id
    assert draft.title == "Your AI automation works in the demo. Then real life shows up."
    assert draft.hook == "Your AI automation works perfectly in the demo. Then one weird email arrives."
    assert draft.estimated_duration_seconds == voiceover.target_duration_seconds
    assert draft.retention_beats == (
        "The name is missing. The format is different. The request is unclear.",
        "Suddenly the automation does the wrong thing confidently.",
        "That is not an AI problem. That is a workflow problem.",
        "Decide what goes through, what gets reviewed, and what must stop.",
    )
    assert draft.caption == draft.hook
    assert "exception path" not in " ".join(draft.retention_beats).casefold()


def test_validate_final_voiceover_script_rejects_bad_language_duration_and_filler():
    script = FinalVoiceoverScript(
        candidate_id="candidate",
        text="In this video we will unlock your full potential with a very long and generic intro.",
        language="de",
        voice="en-GB-RyanNeural",
        target_duration_seconds=5,
    )

    validation = validate_final_voiceover_script(script)

    assert validation.is_valid is False
    assert "final voiceover language must be en" in validation.errors
    assert "target_duration_seconds must be between 15 and 45" in validation.errors
    assert "voiceover contains generic intro/filler language" in validation.errors
