"""Settings, TTS routes, routing log, server logs, and voice design presets endpoints."""
from __future__ import annotations

from fastapi import APIRouter, HTTPException, Request

from core.config import _load_settings, _save_settings, _normalize_settings, _SETTINGS_KEYS
from core.routing import _load_tts_routes, _save_tts_routes
from core.constants import _log_buffer, _LOG_BUFFER_MAX, _routing_log, _ROUTING_LOG_MAX
from core.presets import _load_design_presets, _save_design_presets

router = APIRouter()


@router.get("/api/settings")
async def get_settings():
    return _load_settings()


@router.post("/api/settings")
async def post_settings(request: Request):
    data = await request.json()
    s = _load_settings()
    s.update({k: v for k, v in data.items() if k in _SETTINGS_KEYS})
    s = _normalize_settings(s)
    _save_settings(s)
    return {"ok": True}


@router.get("/api/tts-routes")
async def get_tts_routes():
    return {"routes": _load_tts_routes()}


@router.post("/api/tts-routes")
async def post_tts_routes(request: Request):
    data = await request.json()
    routes = data.get("routes", data) if isinstance(data, dict) else data
    if not isinstance(routes, list):
        raise HTTPException(400, "routes must be a list")
    _save_tts_routes(routes)
    return {"ok": True, "routes": _load_tts_routes()}


@router.get("/api/tts-routing-log")
async def get_tts_routing_log(limit: int = 60):
    limit = max(1, min(int(limit or 60), _ROUTING_LOG_MAX))
    return {"items": _routing_log[:limit], "max": _ROUTING_LOG_MAX}


@router.delete("/api/tts-routing-log")
async def clear_tts_routing_log():
    _routing_log.clear()
    return {"ok": True, "items": []}


@router.get("/api/logs")
async def get_server_logs(limit: int = 200):
    limit = max(1, min(int(limit or 200), _LOG_BUFFER_MAX))
    return {"items": _log_buffer[:limit], "max": _LOG_BUFFER_MAX}


@router.delete("/api/logs")
async def clear_server_logs():
    _log_buffer.clear()
    return {"ok": True}


@router.get("/api/voice-design-presets")
async def get_voice_design_presets():
    return _load_design_presets()


@router.post("/api/voice-design-presets")
async def post_voice_design_presets(request: Request):
    data = await request.json()
    if not isinstance(data, dict):
        raise HTTPException(400, "Expected a preset object")
    _save_design_presets(data)
    return {"ok": True, "presets": _load_design_presets()}
