"""Script drafts generated from content briefs before render planning."""

from __future__ import annotations

from dataclasses import dataclass

from autoshorts.content.brief import ContentBrief
from autoshorts.ideas.candidate import ValidationResult

_GENERIC_AI_SLOP_PHRASES = (
    "in this video",
    "unlock your full potential",
    "game changer",
    "revolutionize your life",
    "boost productivity",
)


@dataclass(frozen=True)
class ScriptDraft:
    brief_id: str
    title: str
    hook: str
    script_outline: str
    retention_beats: tuple[str, ...]
    visual_concept: str
    caption: str
    cta: str
    estimated_duration_seconds: int


def _is_blank(value: str) -> bool:
    return not (value or "").strip()


def create_script_draft(
    brief: ContentBrief,
    *,
    hook: str | None = None,
    script_outline: str | None = None,
    retention_beats: tuple[str, ...] | None = None,
    visual_concept: str | None = None,
    caption: str | None = None,
    cta: str | None = None,
    estimated_duration_seconds: int | None = None,
) -> ScriptDraft:
    beats = retention_beats or (
        f"Show the messy current state: {brief.audience_problem}",
        "Reveal the control rule: AI suggests, human approves.",
        f"Walk through the payoff: {brief.payoff}.",
        "End on the exact comment keyword for the checklist.",
    )

    return ScriptDraft(
        brief_id=brief.brief_id,
        title=brief.topic,
        hook=hook if hook is not None else "If AI runs your week, you have already lost control.",
        script_outline=script_outline
        if script_outline is not None
        else (
            f"Open with the risk of letting AI steer priorities. Show a simple human approval checkpoint, "
            f"then demonstrate {brief.payoff}. Keep the final decision human."
        ),
        retention_beats=beats,
        visual_concept=visual_concept
        if visual_concept is not None
        else (
            "Fast desk-to-phone sequence with a visible human approval checkpoint, "
            "calendar card, checklist beat, and no generated fake UI text."
        ),
        caption=caption if caption is not None else "Keep AI useful by keeping the final decision human.",
        cta=cta if cta is not None else "Comment REVIEW if you want the weekly checklist.",
        estimated_duration_seconds=(
            estimated_duration_seconds if estimated_duration_seconds is not None else brief.target_duration_seconds
        ),
    )


def validate_script_draft(draft: ScriptDraft) -> ValidationResult:
    errors: list[str] = []
    required = {
        "brief_id": draft.brief_id,
        "title": draft.title,
        "hook": draft.hook,
        "script_outline": draft.script_outline,
        "visual_concept": draft.visual_concept,
        "caption": draft.caption,
        "cta": draft.cta,
    }
    for field_name, value in required.items():
        if _is_blank(value):
            errors.append(f"{field_name} is required")

    if not 3 <= len(draft.retention_beats) <= 5:
        errors.append("retention_beats must contain 3 to 5 beats")
    for index, beat in enumerate(draft.retention_beats, start=1):
        if _is_blank(beat):
            errors.append(f"retention beat {index} is required")

    if not 10 <= draft.estimated_duration_seconds <= 180:
        errors.append("duration_seconds must be between 10 and 180")

    combined_text = " ".join(
        [draft.title, draft.hook, draft.script_outline, draft.visual_concept, draft.caption, draft.cta]
        + list(draft.retention_beats)
    ).casefold()
    for phrase in _GENERIC_AI_SLOP_PHRASES:
        if phrase in combined_text:
            errors.append(f"generic AI-slop phrase: {phrase}")

    return ValidationResult(is_valid=not errors, errors=tuple(errors))
