"""Tests for prompt builder and JSON schema."""
import json
import pytest
from src.prompt_builder import (
    build_system_prompt, get_user_prompt, get_json_schema,
    TranscriptionSchema,
)


def test_system_prompt_contains_language():
    prompt = build_system_prompt("te")
    assert "Telugu" in prompt
    assert "te" in prompt


def test_system_prompt_contains_rules():
    prompt = build_system_prompt("hi")
    assert "NEVER TRANSLATE" in prompt
    assert "VERBATIM" in prompt
    assert "NO HALLUCINATION" in prompt
    assert "[UNK]" in prompt
    assert "[NO_SPEECH]" in prompt


def test_system_prompt_contains_event_tags():
    prompt = build_system_prompt("en")
    assert "[laugh]" in prompt
    assert "[cough]" in prompt
    assert "[noise]" in prompt


def test_system_prompt_all_languages():
    from src.config import SUPPORTED_LANGUAGES
    for lang in SUPPORTED_LANGUAGES:
        prompt = build_system_prompt(lang)
        assert len(prompt) > 200


def test_system_prompt_unknown_lang_defaults_english():
    prompt = build_system_prompt("xx")
    assert "English" in prompt


def test_user_prompt():
    p = get_user_prompt()
    assert "Transcribe" in p
    assert "JSON" in p


def test_json_schema_structure():
    schema = get_json_schema()
    assert schema["type"] == "object"
    props = schema["properties"]
    assert "transcription" in props
    assert "tagged" in props
    assert "speaker" in props
    assert "detected_language" in props
    assert set(schema["required"]) == {"transcription", "tagged", "speaker", "detected_language"}


def test_json_schema_speaker_nested():
    schema = get_json_schema()
    speaker = schema["properties"]["speaker"]
    assert speaker["type"] == "object"
    sp = speaker["properties"]
    assert "emotion" in sp
    assert "speaking_style" in sp
    assert "pace" in sp
    assert "accent" in sp


def test_json_schema_enums():
    schema = get_json_schema()
    emotion = schema["properties"]["speaker"]["properties"]["emotion"]
    assert "enum" in emotion
    assert "neutral" in emotion["enum"]
    assert "happy" in emotion["enum"]


def test_pydantic_model_validates():
    data = {
        "transcription": "hello world",
        "tagged": "[noise] hello world",
        "speaker": {
            "emotion": "neutral",
            "speaking_style": "conversational",
            "pace": "normal",
            "accent": "",
        },
        "detected_language": "en",
    }
    model = TranscriptionSchema(**data)
    assert model.transcription == "hello world"
    assert model.speaker.emotion.value == "neutral"


def test_json_schema_no_refs():
    """Ensure schema has no $ref pointers (fully resolved)."""
    schema_str = json.dumps(get_json_schema())
    assert "$ref" not in schema_str
