from pathlib import Path

from autoshorts.growth_plan import load_testbatch
from autoshorts.script_drafts import build_script_draft, format_telegram_review, write_review_drafts


def test_build_script_draft_has_timed_sections_and_platform_note():
    batch = load_testbatch(Path("data/drafts/testbatch_001.json"))
    idea = batch["ideas"][0]

    draft = build_script_draft(idea)

    assert draft["id"] == idea["id"]
    assert draft["duration_seconds"] == idea["duration_seconds"]
    assert draft["sections"][0]["label"] == "Hook"
    assert draft["sections"][0]["text"] == idea["hook"]
    assert sum(section["seconds"] for section in draft["sections"]) == idea["duration_seconds"]
    assert draft["platform_note"] == "Growth Short"
    assert draft["hashtags"]


def test_build_script_draft_marks_tiktok_rewards_candidate():
    batch = load_testbatch(Path("data/drafts/testbatch_001.json"))
    idea = next(item for item in batch["ideas"] if item["duration_seconds"] >= 60)

    draft = build_script_draft(idea)

    assert draft["platform_note"] == "TikTok Rewards Candidate"
    assert draft["sections"][-1]["label"] == "CTA"


def test_format_telegram_review_contains_approval_commands():
    batch = load_testbatch(Path("data/drafts/testbatch_001.json"))
    draft = build_script_draft(batch["ideas"][0])

    text = format_telegram_review(draft)

    assert "## AutoShortsBot Review" in text
    assert "Freigabe alle" in text
    assert draft["hook"] in text
    assert "Retention Score" in text
    assert "Quality Score" in text


def test_write_review_drafts_creates_one_markdown_per_idea(tmp_path):
    batch = load_testbatch(Path("data/drafts/testbatch_001.json"))

    paths = write_review_drafts(batch, tmp_path)

    assert len(paths) == 9
    first = paths[0].read_text(encoding="utf-8")
    assert "AutoShortsBot Review" in first
    assert paths[0].suffix == ".md"
