"""System-wide voice configuration stored in the admin user's settings row.

Falls back to Config env vars so operators can still seed defaults via the
environment without touching the UI.
"""
from sqlalchemy import select

from fabledassistant.config import Config
from fabledassistant.models import async_session
from fabledassistant.models.setting import Setting
from fabledassistant.models.user import User

VOICE_SETTING_KEYS = ("voice_enabled", "voice_stt_model")


async def get_voice_config() -> dict[str, str]:
    """Return voice config, preferring DB values over env-var defaults."""
    config: dict[str, str] = {
        "voice_enabled": "true" if Config.VOICE_ENABLED else "false",
        "voice_stt_model": Config.STT_MODEL,
    }
    async with async_session() as session:
        result = await session.execute(
            select(Setting)
            .join(User, Setting.user_id == User.id)
            .where(User.role == "admin", Setting.key.in_(VOICE_SETTING_KEYS))
        )
        for setting in result.scalars().all():
            config[setting.key] = setting.value
    return config


async def is_voice_enabled() -> bool:
    config = await get_voice_config()
    return config.get("voice_enabled", "false").lower() in ("1", "true", "yes")


async def get_stt_model() -> str:
    config = await get_voice_config()
    return config.get("voice_stt_model", Config.STT_MODEL)
