"""Mode-specific system prompts for Tea Party script generation."""

from __future__ import annotations

from alice_cli.teaparty.models import GenerationMode

# ── Language name mapping ─────────────────────────────────────────────────────

_LANGUAGE_NAMES: dict[str, str] = {
    "af": "Afrikaans",
    "ar": "Arabic",
    "ca": "Catalan",
    "cs": "Czech",
    "cy": "Welsh",
    "da": "Danish",
    "de": "German",
    "de-AT": "Austrian German",
    "el": "Greek",
    "en-AU": "Australian English",
    "en-GB": "British English",
    "en-IN": "Indian English",
    "en-NZ": "New Zealand English",
    "en-ZA": "South African English",
    "es": "Spanish",
    "es-ES": "European Spanish",
    "es-MX": "Mexican Spanish",
    "es-US": "US Spanish",
    "fi": "Finnish",
    "fr": "French",
    "fr-BE": "Belgian French",
    "fr-CA": "Canadian French",
    "he": "Hebrew",
    "hi": "Hindi",
    "hu": "Hungarian",
    "id": "Indonesian",
    "is": "Icelandic",
    "it": "Italian",
    "ja": "Japanese",
    "ko": "Korean",
    "ms": "Malay",
    "nb": "Norwegian Bokmål",
    "nl": "Dutch",
    "nl-BE": "Belgian Dutch",
    "pl": "Polish",
    "pt": "Portuguese",
    "pt-BR": "Brazilian Portuguese",
    "pt-PT": "European Portuguese",
    "ro": "Romanian",
    "ru": "Russian",
    "sk": "Slovak",
    "sv": "Swedish",
    "th": "Thai",
    "tr": "Turkish",
    "uk": "Ukrainian",
    "vi": "Vietnamese",
    "zh-CN": "Simplified Chinese",
    "zh-TW": "Traditional Chinese",
}


def _language_name(lang: str) -> str:
    """Return a human-readable language name for a BCP-47 code."""
    if lang in _LANGUAGE_NAMES:
        return _LANGUAGE_NAMES[lang]
    # Fall back to the code itself so the model still gets a hint.
    return lang


# ── Shared fragments ──────────────────────────────────────────────────────────

_JSON_INSTRUCTION = (
    "Return the script as a JSON object with a single key `turns` containing an array. "
    'Each element must have `speaker` (string: "Host_A" or "Host_B") and `text` '
    "(string: the dialogue line) fields. Return ONLY the JSON object — no markdown "
    "fences, no commentary."
)

_QUALITY_INSTRUCTION = (
    "Avoid filler phrases, meta-commentary about being an AI, and unnatural transitions. "
    "Do not reference the source material as a document — speak as if you know the topic naturally."
)


def _lang_instruction(lang: str) -> str:
    """Return a language instruction line, or empty string for en-US."""
    if lang == "en-US":
        return ""
    name = _language_name(lang)
    return f"Generate the script in {name}."


# ── Mode prompts ──────────────────────────────────────────────────────────────

_CONVERSATION_PROMPT = (
    "You are a podcast script writer. Produce a two-speaker conversational summary "
    "of the provided source material.\n\n"
    "Speakers:\n"
    "- Host_A: the knowledgeable explainer who breaks down concepts clearly.\n"
    "- Host_B: the curious questioner who asks follow-ups and reacts naturally.\n\n"
    "Rules:\n"
    "- Produce a minimum of 6 turns.\n"
    "- Alternate speakers, starting with Host_A.\n"
    f"- {_QUALITY_INSTRUCTION}\n\n"
    f"{_JSON_INSTRUCTION}"
)

_NARRATOR_PROMPT = (
    "You are a podcast script writer. Produce a single-voice narrative summary "
    "of the provided source material, suitable for audiobook-style narration.\n\n"
    "Speaker:\n"
    "- All turns use Host_A as the sole narrator.\n\n"
    "Rules:\n"
    "- Use clear topic transitions and varied sentence structure.\n"
    "- Maintain a professional, engaging tone suitable for audio consumption.\n"
    f"- {_QUALITY_INSTRUCTION}\n\n"
    f"{_JSON_INSTRUCTION}"
)

_BRIEFING_PROMPT = (
    "You are a podcast script writer. Produce a structured news-style briefing "
    "of the provided source material.\n\n"
    "Speakers:\n"
    "- Host_A: the anchor who introduces and segments topics.\n"
    "- Host_B: the analyst who provides brief commentary and context.\n\n"
    "Rules:\n"
    "- Organize the material into distinct topics.\n"
    "- Each topic is introduced by Host_A and discussed briefly with Host_B.\n"
    "- Open with a brief overview of topics to be covered.\n"
    "- Close with a concise wrap-up.\n"
    f"- {_QUALITY_INSTRUCTION}\n\n"
    f"{_JSON_INSTRUCTION}"
)

_INTERVIEW_PROMPT = (
    "You are a podcast script writer. Produce an interview-style dialogue "
    "about the provided source material.\n\n"
    "Speakers:\n"
    "- Host_A: the interviewer who asks probing, progressively deeper questions.\n"
    "- Host_B: the subject-matter expert who answers based on the source material.\n\n"
    "Rules:\n"
    "- Host_A asks progressively deeper questions, moving from overview to specifics.\n"
    "- Host_B answers as if they authored the source material, using first-person "
    "perspective where appropriate.\n"
    f"- {_QUALITY_INSTRUCTION}\n\n"
    f"{_JSON_INSTRUCTION}"
)

_MODE_PROMPTS: dict[GenerationMode, str] = {
    GenerationMode.CONVERSATION: _CONVERSATION_PROMPT,
    GenerationMode.NARRATOR: _NARRATOR_PROMPT,
    GenerationMode.BRIEFING: _BRIEFING_PROMPT,
    GenerationMode.INTERVIEW: _INTERVIEW_PROMPT,
}


# ── Public API ────────────────────────────────────────────────────────────────


def get_system_prompt(mode: GenerationMode, lang: str = "en-US") -> str:
    """Return the system prompt for the given generation mode.

    Args:
        mode: The generation mode.
        lang: Target language code for script generation.

    Returns:
        System prompt string instructing the model to generate a script
        in the specified style and language.
    """
    base = _MODE_PROMPTS[mode]
    lang_line = _lang_instruction(lang)
    if lang_line:
        return f"{base}\n\n{lang_line}"
    return base
