"""Speech engine constants and text-processing utilities."""

from __future__ import annotations

import re
from pathlib import Path

# ── Defaults ────────────────────────────────────────────────────────────────

DEFAULT_WHISPER_MODEL = "base.en"
DEFAULT_PIPER_VOICE = Path.home() / ".local/share/piper-voices/en_US-lessac-medium.onnx"

# ── Voice-mode system instruction ───────────────────────────────────────────
# Prepended to user messages so Timmy responds naturally for TTS.
_VOICE_PREAMBLE = (
    "[VOICE MODE] You are speaking aloud through a text-to-speech system. "
    "Respond in short, natural spoken sentences. No markdown, no bullet points, "
    "no asterisks, no numbered lists, no headers, no bold/italic formatting. "
    "Talk like a person in a conversation — concise, warm, direct. "
    "Keep responses under 3-4 sentences unless the user asks for detail."
)


def _strip_markdown(text: str) -> str:
    """Remove markdown formatting so TTS reads naturally.

    Strips: **bold**, *italic*, `code`, # headers, - bullets,
    numbered lists, [links](url), etc.
    """
    if not text:
        return text
    # Remove bold/italic markers
    text = re.sub(r"\*{1,3}([^*]+)\*{1,3}", r"\1", text)
    # Remove inline code
    text = re.sub(r"`([^`]+)`", r"\1", text)
    # Remove headers (# Header)
    text = re.sub(r"^#{1,6}\s+", "", text, flags=re.MULTILINE)
    # Remove bullet points (-, *, +) at start of line
    text = re.sub(r"^[\s]*[-*+]\s+", "", text, flags=re.MULTILINE)
    # Remove numbered lists (1. 2. etc)
    text = re.sub(r"^[\s]*\d+\.\s+", "", text, flags=re.MULTILINE)
    # Remove link syntax [text](url) → text
    text = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", text)
    # Remove horizontal rules
    text = re.sub(r"^[-*_]{3,}\s*$", "", text, flags=re.MULTILINE)
    # Collapse multiple newlines
    text = re.sub(r"\n{3,}", "\n\n", text)
    return text.strip()
