"""
Script Analyzer Agent – breaks a story into structured scenes.
"""

from __future__ import annotations

from models import SceneBreakdown, TargetPlatform
from agents.base_agent import BaseAgent


SYSTEM_PROMPT = """\
You are an expert video script analyst specializing in short-form viral content.

Given a story or narrative, break it down into NARRATIVE BEATS. Each beat
represents a key moment, emotion shift, or story progression point. These beats
will later be expanded into multiple visual frames with different camera angles.

For EACH narrative beat you must determine:
1. The physical environment / setting (be SPECIFIC and VISUAL — describe colors,
   textures, lighting conditions, objects in the scene)
2. The character's pose, body position, and gesture (specific and cinematic —
   describe exactly what the body is doing)
3. The mood / emotional tone (use cinematic language)
4. The exact narration text that will be spoken as voiceover
5. Delivery notes (pace, emotion, pauses, emphasis)

LANGUAGE RULES (MANDATORY):
- ALL narration_text MUST be written in Brazilian Portuguese (pt-BR), regardless of the input story language
- If the story is in another language, TRANSLATE the narration to natural, conversational Brazilian Portuguese
- Use Brazilian Portuguese contractions, expressions, and rhythm (e.g., "você", "né", "tá")

Rules:
- The FIRST beat MUST be a powerful hook that grabs attention instantly
- Each beat should represent a DISTINCT story moment or emotional shift
- Keep narration sentences short and punchy (max 12 words each)
- Write narration in a conversational, viral social media tone
- Use contractions, rhetorical questions, dramatic pauses ("...")
- Total video should be {duration_hint}
- Generate {scene_count_hint} narrative beats
- Each beat will be expanded into 2-4 visual frames, so write enough detail
  for multiple camera angles per beat
- The narration text is what will be converted to speech via TTS
- Build a clear EMOTIONAL ARC: hook → escalation → climax → resolution
- Each beat's environment should PROGRESS the story visually (don't repeat settings)

Return a structured SceneBreakdown with all beats in order.\
"""


PLATFORM_HINTS: dict[str, tuple[str, str]] = {
    "tiktok": ("45-60 seconds", "8-12"),
    "instagram_reels": ("30-60 seconds", "7-10"),
    "youtube_shorts": ("45-60 seconds", "10-14"),
    "youtube": ("90-180 seconds", "15-25"),
}


class ScriptAnalyzerAgent(BaseAgent):
    """Analyzes a story and produces a SceneBreakdown."""

    def analyze(
        self,
        story: str,
        target_platform: TargetPlatform = TargetPlatform.TIKTOK,
    ) -> SceneBreakdown:
        duration_hint, scene_hint = PLATFORM_HINTS.get(
            target_platform.value, ("30-60 seconds", "8-15")
        )
        system = SYSTEM_PROMPT.format(
            duration_hint=duration_hint,
            scene_count_hint=scene_hint,
        )
        user = f"Story / Narrative:\n\n{story}"
        return self.call(
            system_prompt=system,
            user_prompt=user,
            response_model=SceneBreakdown,
            temperature=0.7,
        )
