import json

from autoshorts.strategy.format_family import (
    CTAType,
    ClaimMode,
    DurationClass,
    EvidenceLevel,
    FormatCandidatePlan,
    FormatMetadata,
    FormatType,
    HookType,
    ViewerState,
    build_first_test_wave,
    default_format_template,
    format_mix_summary,
    render_test_wave_json,
    validate_format_candidate_plan,
    validate_format_metadata,
)


def test_format_mix_matches_strategy_for_next_20_videos():
    assert format_mix_summary() == {
        "red_flag_short": 10,
        "already_clicked": 4,
        "true_scam_story": 3,
        "spot_the_trap_quiz": 2,
        "scam_breakdown": 1,
    }


def test_templates_define_duration_cta_and_website_targets():
    recovery = default_format_template(FormatType.ALREADY_CLICKED)
    quiz = default_format_template("spot_the_trap_quiz")

    assert recovery.default_hook_type == HookType.RECOVERY
    assert recovery.default_viewer_state == ViewerState.ALREADY_CLICKED
    assert recovery.default_cta_type == CTAType.CHECKLIST
    assert recovery.duration_class == DurationClass.RECOVERY_50_75
    assert recovery.website_target_pattern == "/already-clicked/"
    assert "stop doing more" in " ".join(recovery.structure)

    assert quiz.default_cta_type == CTAType.COMMENT
    assert "Can you spot" in quiz.structure[0]


def test_format_metadata_validates_recovery_and_first_frame_label():
    metadata = FormatMetadata(
        format_type=FormatType.ALREADY_CLICKED,
        hook_type=HookType.RECOVERY,
        viewer_state=ViewerState.NOT_CLICKED,
        website_target="already-clicked",
        cta_type=CTAType.CHECKLIST,
        duration_class=DurationClass.RECOVERY_50_75,
        first_frame_scam_label="",
        evidence_level=EvidenceLevel.GENERIC_PATTERN,
        claim_mode=ClaimMode.EDUCATIONAL_PATTERN,
    )

    result = validate_format_metadata(metadata)

    assert not result.is_valid
    assert "website_target must be a site path" in result.errors
    assert "first_frame_scam_label is required" in result.errors
    assert "already_clicked format requires viewer_state=already_clicked" in result.errors


def test_first_test_wave_is_valid_and_contains_requested_formats():
    wave = build_first_test_wave()

    assert [plan.format_metadata.format_type for plan in wave] == [
        FormatType.ALREADY_CLICKED,
        FormatType.SPOT_THE_TRAP_QUIZ,
        FormatType.TRUE_SCAM_STORY,
        FormatType.SCAM_BREAKDOWN,
        FormatType.ALREADY_CLICKED,
    ]
    assert all(validate_format_candidate_plan(plan).is_valid for plan in wave)
    assert wave[0].format_metadata.website_target == "/already-clicked/"
    assert wave[0].format_metadata.first_frame_scam_label == "FAKE DELIVERY LINK"
    assert "only on first keyframe" in wave[0].first_frame_brief


def test_plan_serializes_format_metadata_for_audits():
    plan = build_first_test_wave()[0]
    data = plan.to_dict()

    assert data["format_metadata"] == {
        "format_type": "already_clicked",
        "hook_type": "recovery",
        "viewer_state": "already_clicked",
        "website_target": "/already-clicked/",
        "cta_type": "checklist",
        "duration_class": "50-75",
        "first_frame_scam_label": "FAKE DELIVERY LINK",
        "evidence_level": "generic_pattern",
        "claim_mode": "educational_pattern",
    }

    parsed = json.loads(render_test_wave_json([plan]))
    assert parsed[0]["candidate_id"] == "tt-recovery-fake-delivery-link"


def test_candidate_plan_requires_red_first_frame_brief():
    plan = FormatCandidatePlan(
        candidate_id="x",
        title="Already clicked?",
        hook="Already clicked?",
        topic_slug="delivery-sms-trap",
        format_metadata=build_first_test_wave()[0].format_metadata,
        script_beats=("stop",),
        first_frame_brief="Premium phone screen with the scam name.",
        cta="Checklist",
    )

    result = validate_format_candidate_plan(plan)

    assert not result.is_valid
    assert "first_frame_brief must mention red first-frame scam label" in result.errors
