from autoshorts.rendering.keyframe_first_plan import (
    KeyframeRole,
    WanMode,
    build_s03_keyframe_first_plan,
    validate_keyframe_first_scene_plan,
)


def test_s03_keyframe_first_plan_has_start_and_end_frames():
    plan = build_s03_keyframe_first_plan("ai-output-autopsy-decision-summary-38s")

    assert plan.scene_id == "s03_mechanism"
    assert plan.start_keyframe.role is KeyframeRole.START_FRAME
    assert plan.end_keyframe is not None
    assert plan.end_keyframe.role is KeyframeRole.END_FRAME
    assert plan.wan_mode is WanMode.FIRST_LAST_FRAME_TO_VIDEO


def test_keyframe_specs_forbid_readable_text_and_fake_ui():
    plan = build_s03_keyframe_first_plan("ai-output-autopsy-decision-summary-38s")

    for spec in (plan.start_keyframe, plan.end_keyframe):
        assert "readable text" in spec.forbidden_elements
        assert "fake UI text" in spec.forbidden_elements
        assert "logos" in spec.forbidden_elements
        assert spec.approval_required is True


def test_s03_keyframes_are_semantically_grounded_in_meeting_summary_mechanism():
    plan = build_s03_keyframe_first_plan("ai-output-autopsy-decision-summary-38s")

    joined = " ".join([
        plan.start_keyframe.visual_goal,
        plan.start_keyframe.composition,
        plan.end_keyframe.visual_goal if plan.end_keyframe else "",
        plan.expected_motion,
    ]).lower()

    assert "meeting" in joined
    assert "compression" in joined
    assert "decision-target" in joined or "decision target" in joined
    assert "summary" in joined


def test_keyframe_first_plan_limits_renderer_owned_overlays():
    plan = build_s03_keyframe_first_plan("ai-output-autopsy-decision-summary-38s")

    assert plan.max_allowed_overlays <= 2
    assert plan.start_keyframe.renderer_owned_overlays == ("small arrow", "small NO DECISION TARGET label", "captions")


def test_validate_keyframe_first_scene_plan_rejects_text_to_video_only():
    plan = build_s03_keyframe_first_plan("ai-output-autopsy-decision-summary-38s")
    invalid = plan.__class__(
        scene_id=plan.scene_id,
        start_keyframe=plan.start_keyframe,
        end_keyframe=None,
        wan_mode=WanMode.TEXT_TO_VIDEO,
        duration_seconds=plan.duration_seconds,
        expected_motion=plan.expected_motion,
        max_allowed_overlays=plan.max_allowed_overlays,
    )

    result = validate_keyframe_first_scene_plan(invalid)

    assert result.allowed is False
    assert result.reason == "text_to_video_only_disallowed"
