import json
from pathlib import Path

from autoshorts.asset_plan import build_asset_plan, write_asset_plans
from autoshorts.mechanism_pipeline import build_mechanism_shot_plan
from autoshorts.render_manifest import build_render_manifest
from autoshorts.retention_studio import PILOT_IDEAS, build_mechanism_explainer


def _manifest(name: str = "ai-004-stop-asking-ai-to-think-for-you") -> dict:
    path = Path("data/render_manifests/testbatch_002_top3") / f"{name}.json"
    return json.loads(path.read_text(encoding="utf-8"))


def test_build_asset_plan_replaces_static_cards_with_moving_asset_modes():
    manifest = _manifest()

    plan = build_asset_plan(manifest)

    assert plan["schema_version"] == "asset_plan.v2"
    assert plan["video"]["id"] == "ai-004"
    assert len(plan["scenes"]) == 5
    modes = [scene["asset_mode"] for scene in plan["scenes"]]
    assert "cinematic_ai_video" in modes
    assert "screen_recording_simulation" in modes
    assert "kinetic_text_overlay" in modes
    assert "static_card" not in modes
    assert all(scene["motion_directive"] for scene in plan["scenes"])
    assert all(scene["overlay_text"] for scene in plan["scenes"])


def test_cinematic_ai_video_scenes_have_provider_ready_prompts_without_baked_text():
    manifest = _manifest()

    plan = build_asset_plan(manifest)

    cinematic = [scene for scene in plan["scenes"] if scene["asset_mode"] == "cinematic_ai_video"]
    assert cinematic
    for scene in cinematic:
        prompt = scene["generation_prompt"]
        assert scene["provider_family"] == "local_free_renderer"
        assert scene["no_text_in_video"] is True
        assert "cinematic-looking" in prompt.lower()
        assert "vertical 9:16" in prompt.lower()
        assert "paid ai-video credits" in prompt.lower()
        assert "no readable text" in scene["negative_prompt"].lower()
        assert scene["duration_seconds"] >= 3
        assert scene["duration_seconds"] <= 6


def test_attention_design_asset_plan_uses_broll_and_screen_recording_not_text_slideshow():
    manifest = _manifest("attention-002-the-algorithm-does-not-need-you-happy")

    plan = build_asset_plan(manifest)

    modes = [scene["asset_mode"] for scene in plan["scenes"]]
    assert "cinematic_ai_video" in modes
    assert "screen_recording_simulation" in modes
    assert "animated_card" in modes
    assert modes.count("kinetic_text_overlay") <= 2
    assert plan["final_render_strategy"]["static_card_limit"] == 0


def test_write_asset_plans_outputs_json_files(tmp_path):
    manifests = [_manifest(), _manifest("attention-002-the-algorithm-does-not-need-you-happy")]

    paths = write_asset_plans(manifests, tmp_path)

    assert len(paths) == 2
    assert all(path.suffix == ".json" for path in paths)
    payload = json.loads(paths[0].read_text(encoding="utf-8"))
    assert payload["schema_version"] == "asset_plan.v2"
    assert payload["scenes"][0]["scene_id"] == "scene_01"


def test_asset_plan_maps_mechanism_manifest_to_motion_explainer_modes():
    explainer = build_mechanism_explainer(PILOT_IDEAS[0])
    manifest = build_render_manifest(build_mechanism_shot_plan(explainer))

    plan = build_asset_plan(manifest)

    modes = [scene["asset_mode"] for scene in plan["scenes"]]
    assert modes == [
        "cinematic_ai_video",
        "cinematic_ai_video",
        "mechanism_animation",
        "mechanism_animation",
        "kinetic_text_overlay",
    ]
    assert plan["final_render_strategy"]["preferred_mix"]["mechanism_animation"] == 2
    mechanism_scene = plan["scenes"][2]
    assert mechanism_scene["provider_family"] == "local_free_renderer"
    assert "Röntgen" in mechanism_scene["generation_prompt"] or "x-ray" in mechanism_scene["generation_prompt"]
