from autoshorts.ideas.candidate import VideoCandidate
from autoshorts.scripting.quality import ScriptQualityScore
from autoshorts.strategy.brand import (
    TRUETRACE_SHORTS_BRAND,
    BrandProfile,
    apply_brand_metadata,
    validate_brand_profile,
    validate_candidate_brand_fit,
)


def _candidate(**overrides):
    values = {
        "candidate_id": "prompt-vs-system-001",
        "pillar": "ai_life_systems",
        "title": "Prompt vs System: why the prompt is not the workflow",
        "platforms": ("youtube", "tiktok"),
        "duration_seconds": 35,
        "hook": "This prompt looks clever, but the workflow still breaks.",
        "script_outline": "Show the fragile prompt, reveal the missing boundary, then replace it with a repeatable system check.",
        "visual_concept": "Dark mechanism diagram with trace lines, prompt card, workflow boundary, and verdict stamp.",
        "caption": "A prompt is not a system. Trace the workflow before you automate it.",
        "cta": "Follow for practical AI systems, not prompt worship.",
        "hashtags": ("#AI", "#AISystems", "#Workflow"),
        "quality_score": ScriptQualityScore(
            hook_strength=9,
            novelty=9,
            clarity=9,
            retention=8,
            ai_slop_risk=2,
            policy_risk="low",
        ),
        "policy_notes": ("duration_class: growth_short",),
        "hypothesis": "Systems-over-prompts framing should attract serious AI workflow builders.",
    }
    values.update(overrides)
    return VideoCandidate(**values)


def test_truetrace_brand_profile_defines_safe_public_positioning():
    brand = TRUETRACE_SHORTS_BRAND

    assert isinstance(brand, BrandProfile)
    assert brand.brand_id == "truetrace_shorts"
    assert brand.channel_name == "True Trace Shorts"
    assert brand.youtube_handle == "@TrueTraceShorts"
    assert brand.tiktok_handle == "@truetraceshorts"
    assert brand.tagline == "Systems over prompts."
    assert "Trace the real mechanism" in brand.positioning
    assert "JARVIS" not in brand.public_youtube_description
    assert "No prompt worship" in brand.public_youtube_description
    assert brand.default_hashtags[:3] == ("#AI", "#AISystems", "#Workflow")
    assert "dark mechanism diagrams" in brand.visual_rules


def test_validate_brand_profile_accepts_truetrace_default():
    result = validate_brand_profile(TRUETRACE_SHORTS_BRAND)

    assert result.is_valid is True
    assert result.errors == ()


def test_validate_candidate_brand_fit_accepts_mechanism_reveal_ai_systems_candidate():
    result = validate_candidate_brand_fit(_candidate(), TRUETRACE_SHORTS_BRAND)

    assert result.is_valid is True
    assert result.errors == ()


def test_validate_candidate_brand_fit_rejects_private_or_political_or_generic_content():
    private_result = validate_candidate_brand_fit(
        _candidate(
            title="How JARVIS and the user built the content machine",
            hook="Behind the scenes of our collaboration.",
            caption="Private build log from JARVIS.",
        ),
        TRUETRACE_SHORTS_BRAND,
    )
    assert private_result.is_valid is False
    assert "candidate uses forbidden public topic: jarvis" in private_result.errors
    assert "candidate uses forbidden public topic: collaboration" in private_result.errors

    political_result = validate_candidate_brand_fit(
        _candidate(
            title="AI campaign strategy for elections",
            hook="This political workflow changes voter persuasion.",
            caption="Campaign automation with AI.",
        ),
        TRUETRACE_SHORTS_BRAND,
    )
    assert political_result.is_valid is False
    assert "candidate uses forbidden public topic: politics" in political_result.errors

    generic_result = validate_candidate_brand_fit(
        _candidate(
            title="5 best AI tools you need today",
            hook="These AI tools will unlock your full potential.",
            script_outline="List five tools with generic benefits.",
            visual_concept="Stock b-roll and floating app logos.",
            caption="Follow for more AI tips.",
            cta="Follow for more AI tips.",
        ),
        TRUETRACE_SHORTS_BRAND,
    )
    assert generic_result.is_valid is False
    assert "candidate does not fit brand promise: mechanisms over hype" in generic_result.errors
    assert "candidate uses generic AI tips positioning" in generic_result.errors


def test_apply_brand_metadata_adds_default_hashtags_and_platform_templates():
    candidate = _candidate(hashtags=("#AI",), caption="Trace the broken workflow boundary.")

    branded = apply_brand_metadata(candidate, TRUETRACE_SHORTS_BRAND)

    assert branded.caption == "Trace the broken workflow boundary."
    assert branded.hashtags == ("#AI", "#AISystems", "#Workflow", "#Productivity")
    assert "Brand: truetrace_shorts" in branded.policy_notes
    assert "Public tagline: Systems over prompts." in branded.policy_notes
    assert branded.candidate_id == candidate.candidate_id
    assert branded.quality_score == candidate.quality_score
