from autoshorts.retention import evaluate_red_flag_video_gates, score_hook, score_idea_retention


def test_score_hook_rewards_specific_tension_and_direct_address():
    weak = score_hook("Here are some productivity tips")
    strong = score_hook("If your brain has 47 tabs open, do this before opening TikTok.")

    assert strong["score"] > weak["score"]
    assert "specificity" in strong["signals"]
    assert "direct_address" in strong["signals"]
    assert "open_loop" in strong["signals"]


def test_score_idea_retention_penalizes_slow_generic_intros():
    idea = {
        "hook": "In this video I will talk about productivity tips.",
        "duration_seconds": 75,
        "visual_concept": "Static slideshow with abstract background.",
        "script_outline": "Some generic advice about habits and routines.",
        "cta": "Follow for more.",
    }

    result = score_idea_retention(idea)

    assert result["score"] < 7
    assert "slow_intro" in result["risks"]
    assert "generic_value" in result["risks"]


def test_score_idea_retention_accepts_strong_testbatch_candidate():
    idea = {
        "hook": "You do not need more discipline. You need a smaller first step.",
        "duration_seconds": 38,
        "visual_concept": "Pattern interrupt: messy desk becomes one tiny next action with timer and captions.",
        "script_outline": "Challenge the discipline myth, show the two-minute starter, give one concrete example, close the loop.",
        "cta": "Comment START if you want the template.",
    }

    result = score_idea_retention(idea)

    assert result["score"] >= 8
    assert not result["risks"]


def test_red_flag_video_gates_require_screen_trap_one_move_and_companion_value():
    candidate = {
        "visibleScreen": "Phone browser screen showing a fake CAPTCHA trap asking for notification permission and risking account alerts.",
        "hook": "What is the trap before you click Allow?",
        "redFlag": "The CAPTCHA asks for browser notification permission.",
        "saferMove": "Block the permission request and close the tab.",
        "website_companion_questions": ["What do I check before acting?", "What do I do if I already clicked?"],
    }

    result = evaluate_red_flag_video_gates(candidate)

    assert result["allowed"] is True
    assert result["gates"]["one_second_recognition"] is True


def test_red_flag_video_gates_block_multiple_red_flags_and_vague_cta():
    result = evaluate_red_flag_video_gates({
        "visibleScreen": "Abstract dark background",
        "hook": "Scams are everywhere.",
        "red_flags": ["Bad link", "Bad phone number"],
        "saferMove": "Be careful.",
        "website_companion_questions": [],
    })

    assert result["allowed"] is False
    assert "one_second_recognition_gate" in result["failures"]
    assert "one_red_flag_gate" in result["failures"]
    assert "website_companion_value_gate" in result["failures"]
