"""Proof Short mode and pretty-empty visual gate for early TrueTrace pilots.

Proof Shorts validate viewer problem, visible fail, and useful rule before any
premium AI-film or Wan-heavy production. They are intentionally lightweight and
mockup/screen-led.
"""

from __future__ import annotations

from dataclasses import dataclass
from enum import StrEnum

from autoshorts.ideas.candidate import ValidationResult


class ProductionMode(StrEnum):
    PROOF_SHORT = "proof_short"
    PREMIUM_EPISODE = "premium_episode"


PRETTY_EMPTY_VISUAL_BLOCKER_ID = "pretty_empty_visual_blocker"


@dataclass(frozen=True)
class PrettyEmptyVisualInput:
    first_second_visual: str
    visible_fail: str
    concrete_problem: str
    stakes: str
    repair_rule: str
    save_or_share_reason: str
    visual_specificity: str


@dataclass(frozen=True)
class ProofShortMode:
    mode: ProductionMode = ProductionMode.PROOF_SHORT
    min_duration_seconds: int = 25
    max_duration_seconds: int = 35
    screen_or_real_problem_led: bool = True
    wan_final_allowed: bool = False
    premium_styleframe_pass_allowed: bool = False
    abstract_metaphors_allowed: bool = False
    goal: str = "Validate format/topic with a fast useful preview before premium production."


def evaluate_pretty_empty_visual_blocker(visual: PrettyEmptyVisualInput) -> ValidationResult:
    """Reject visuals that look premium but do not show a concrete viewer problem.

    A good first pilot visual must answer:
    - What is concretely broken?
    - Where is the error visible?
    - What is at stake?
    - Which simple rule repairs it?
    - Why would the viewer save/share it?
    """

    errors: list[str] = []
    checks = {
        "first_second_visual": visual.first_second_visual,
        "visible_fail": visual.visible_fail,
        "concrete_problem": visual.concrete_problem,
        "stakes": visual.stakes,
        "repair_rule": visual.repair_rule,
        "save_or_share_reason": visual.save_or_share_reason,
        "visual_specificity": visual.visual_specificity,
    }
    for field, value in checks.items():
        if not (value or "").strip():
            errors.append(f"{PRETTY_EMPTY_VISUAL_BLOCKER_ID}: {field} is required")

    combined = " ".join(checks.values()).casefold()
    mood_only_terms = (
        "mood",
        "b-roll",
        "generic ai",
        "glassmorphism atmosphere",
        "abstract",
        "beautiful background",
        "premium but empty",
        "could fit any productivity video",
    )
    if any(term in combined for term in mood_only_terms) and not any(term in combined for term in ("invoice", "payment", "email", "route", "red flag", "changed")):
        errors.append(f"{PRETTY_EMPTY_VISUAL_BLOCKER_ID}: premium mood/B-roll without a concrete visible fail is blocked")

    if "no visible fail" in combined or "without voiceover" in combined and "unclear" in combined:
        errors.append(f"{PRETTY_EMPTY_VISUAL_BLOCKER_ID}: visual must show the problem without relying on voiceover")

    return ValidationResult(is_valid=not errors, errors=tuple(dict.fromkeys(errors)))


def proof_short_mode_default() -> ProofShortMode:
    return ProofShortMode()
