"""Live Gemini Subprocess control endpoints."""

from __future__ import annotations

import asyncio

from fastapi import APIRouter, HTTPException

router = APIRouter()


def _sub_or_503():
    from Project.Sanad.main import live_sub
    if live_sub is None:
        raise HTTPException(503, "Live subprocess not available")
    return live_sub


@router.get("/status")
async def subprocess_status():
    from Project.Sanad.main import live_sub
    if live_sub is None:
        return {"available": False, "state": "unavailable"}
    return live_sub.status()


@router.post("/start")
async def start_subprocess():
    live_sub = _sub_or_503()
    try:
        return await asyncio.to_thread(live_sub.start)
    except RuntimeError as exc:
        raise HTTPException(404, str(exc))


@router.post("/stop")
async def stop_subprocess():
    return await asyncio.to_thread(_sub_or_503().stop)
