import pytest

from autoshorts.config.pillars import DEFAULT_PILLARS
from autoshorts.ideas.factory import (
    CandidateDraft,
    classify_duration,
    create_candidate,
    create_candidate_id,
)
from autoshorts.scripting.quality import ScriptQualityScore


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


def _draft(**overrides) -> CandidateDraft:
    data = {
        "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.",
        "quality_score": _score(),
        "policy_notes": ("No fake earnings claims",),
        "hypothesis": "Values-first AI positioning gets more saves than hype content.",
    }
    data.update(overrides)
    return CandidateDraft(**data)


def test_classify_duration_growth_short():
    assert classify_duration(34) == "growth_short"


def test_classify_duration_monetization_candidate():
    assert classify_duration(68) == "monetization_candidate"


def test_classify_duration_experiment_range():
    assert classify_duration(12) == "experiment"
    assert classify_duration(110) == "experiment"


def test_candidate_id_is_stable_slug_from_pillar_title_and_duration():
    candidate_id = create_candidate_id(
        pillar="ai_life_systems",
        title="AI ist ein schlechter Gott, aber ein guter Diener",
        duration_seconds=34,
    )

    assert candidate_id == "ai_life_systems-ai-ist-ein-schlechter-gott-34s"


def test_create_candidate_adds_id_default_hashtags_and_duration_class_policy_note():
    candidate = create_candidate(_draft(), registry=DEFAULT_PILLARS)

    assert candidate.candidate_id == "ai_life_systems-ai-ist-ein-schlechter-gott-34s"
    assert candidate.hashtags == ("#AI", "#LifeSystems", "#Productivity")
    assert "Duration class: growth_short" in candidate.policy_notes
    assert candidate.pillar == "ai_life_systems"
    assert candidate.platforms == ("youtube", "tiktok")


def test_create_candidate_rejects_missing_hypothesis():
    with pytest.raises(ValueError, match="hypothesis is required"):
        create_candidate(_draft(hypothesis=""), registry=DEFAULT_PILLARS)


def test_create_candidate_rejects_unknown_or_unfit_pillar():
    with pytest.raises(ValueError, match="unknown pillar: unknown"):
        create_candidate(_draft(pillar="unknown"), registry=DEFAULT_PILLARS)


def test_create_candidate_rejects_unfit_platform_for_pillar():
    with pytest.raises(ValueError, match="platform instagram is not configured"):
        create_candidate(
            _draft(platforms=("instagram",)),
            registry=tuple(
                p for p in DEFAULT_PILLARS if p.pillar_id != "ai_life_systems"
            )
            + (DEFAULT_PILLARS[0].__class__(
                pillar_id="ai_life_systems",
                audience=DEFAULT_PILLARS[0].audience,
                positioning=DEFAULT_PILLARS[0].positioning,
                tone=DEFAULT_PILLARS[0].tone,
                hook_patterns=DEFAULT_PILLARS[0].hook_patterns,
                visual_rules=DEFAULT_PILLARS[0].visual_rules,
                no_go=DEFAULT_PILLARS[0].no_go,
                platform_fit=("youtube",),
                monetization_angle=DEFAULT_PILLARS[0].monetization_angle,
            ),),
        )
