from autoshorts.ideas.candidate import VideoCandidate
from autoshorts.ideas.testbatch import TestBatch
from autoshorts.review.render import render_review_batch_summary
from autoshorts.review.workflow import build_review_batch
from autoshorts.scripting.quality import ScriptQualityScore


def _good_score() -> ScriptQualityScore:
    return ScriptQualityScore(
        hook_strength=9,
        novelty=8,
        clarity=9,
        retention=8,
        ai_slop_risk=2,
        policy_risk="low",
    )


def _weak_score() -> ScriptQualityScore:
    return ScriptQualityScore(
        hook_strength=5,
        novelty=5,
        clarity=6,
        retention=5,
        ai_slop_risk=8,
        policy_risk="low",
    )


def _candidate(**overrides) -> VideoCandidate:
    data = {
        "candidate_id": "vid-001",
        "pillar": "ai_life_systems",
        "title": "AI ist ein schlechter Gott, aber ein guter Diener",
        "platforms": ("youtube", "tiktok"),
        "duration_seconds": 34,
        "hook": "AI sollte dir dienen, nicht dich führen.",
        "script_outline": "Show AI chaos, then a controlled human-approved workflow.",
        "visual_concept": "Calm desk setup, explicit approval checkpoint, no fake dashboards.",
        "caption": "KI als Werkzeug, nicht als Wertekompass.",
        "cta": "Follow for practical AI life systems.",
        "hashtags": ("#AI", "#LifeSystems"),
        "quality_score": _good_score(),
        "policy_notes": ("No fake earnings claims",),
        "hypothesis": "Values-first AI positioning gets more saves than hype content.",
    }
    data.update(overrides)
    return VideoCandidate(**data)


def test_render_review_batch_summary_contains_counts_and_sections():
    batch = TestBatch(
        batch_id="batch-001",
        candidates=(
            _candidate(candidate_id="ready", title="Ready title"),
            _candidate(candidate_id="blocked", title="Blocked title", quality_score=_weak_score()),
            _candidate(candidate_id="invalid", title="Invalid title", hook=""),
        ),
    )
    review_batch = build_review_batch(
        batch,
        preview_paths={"ready": "data/local_motion_renders/ready.mp4"},
    )

    text = render_review_batch_summary(review_batch)

    assert "## Review Batch: batch-001" in text
    assert "Ready: 1" in text
    assert "Blocked: 1" in text
    assert "Invalid: 1" in text
    assert "## Ready for review" in text
    assert "Review: ready" in text
    assert "Preview: data/local_motion_renders/ready.mp4" in text
    assert "## Blocked" in text
    assert "blocked — Blocked title" in text
    assert "quality gate blocked candidate" in text
    assert "## Invalid" in text
    assert "invalid — Invalid title" in text
    assert "hook is required" in text


def test_render_review_batch_summary_omits_empty_sections():
    batch = TestBatch(batch_id="only-ready", candidates=(_candidate(candidate_id="ready"),))
    review_batch = build_review_batch(batch)

    text = render_review_batch_summary(review_batch)

    assert "Ready: 1" in text
    assert "Blocked: 0" in text
    assert "Invalid: 0" in text
    assert "## Ready for review" in text
    assert "## Blocked" not in text
    assert "## Invalid" not in text


def test_render_review_batch_summary_has_no_publish_language():
    batch = TestBatch(batch_id="safe", candidates=(_candidate(candidate_id="ready"),))
    review_batch = build_review_batch(batch)

    text = render_review_batch_summary(review_batch)

    assert "publish" not in text.casefold()
    assert "post" not in text.casefold()
