"""Jami-Dialog → Kino-Prompt für Veo / Runway (3D-Kamerafahrt)."""

from __future__ import annotations

import re
from pathlib import Path

_CAMERA_SUFFIX = (
    "Cinematic 3D animated commercial, vertical 9:16, ultra sharp, high detail. "
    "Two young German women — Sarah on the left, Laura on the right — sit facing "
    "each other at a stylish rooftop bar at golden hour, engaged in an excited "
    "conversation about dating. Stylized premium 3D animation (Pixar-quality), "
    "warm rim light, expressive faces, subtle lip movement. "
    "Camera: smooth slow orbital dolly around the two-shot, gentle push-in, "
    "shallow depth of field, cinematic motion blur. "
    "Mood: joyful, modern, trustworthy dating-app promo. "
    "No on-screen text, no logos, no watermarks."
)


def _clean_line(line: str) -> str:
    line = line.strip()
    line = re.sub(r"^(Sarah|Laura|sarah|laura|Stimme\s*[12])\s*[:\-–—]\s*", "", line, flags=re.I)
    return line.strip()


def dialogue_summary(text: str, *, max_chars: int = 420) -> str:
    """Kurzfassung aus Dialog-Text für Video-Prompts."""
    lines: list[str] = []
    for raw in text.splitlines():
        raw = raw.strip()
        if not raw or raw.startswith("#"):
            continue
        cleaned = _clean_line(raw)
        if cleaned:
            lines.append(cleaned)
    if not lines:
        return "Two friends talk excitedly about a new dating app and a weekend date."
    blob = " ".join(lines)
    if len(blob) <= max_chars:
        return blob
    return blob[: max_chars - 1].rsplit(" ", 1)[0] + "…"


def build_video_prompt(dialogue_text: str, *, extra: str = "") -> str:
    """Vollständiger visueller Prompt inkl. 3D-Kamerafahrt."""
    scene = dialogue_summary(dialogue_text)
    parts = [f"Scene: {scene}", _CAMERA_SUFFIX]
    if extra.strip():
        parts.insert(1, extra.strip())
    return " ".join(parts)


def load_dialogue_text(
    *,
    text: str = "",
    file_path: Path | None = None,
    use_weekend_date: bool = False,
) -> str:
    if text.strip():
        return text.strip()
    if file_path and file_path.is_file():
        return file_path.read_text(encoding="utf-8").strip()
    if use_weekend_date:
        from jam.ia1minute import weekend_date_turns

        lines = []
        for sp, txt in weekend_date_turns():
            name = "Sarah" if sp == "sarah" else "Laura"
            lines.append(f"{name}: {txt}")
        return "\n\n".join(lines)
    return ""
