"""
Wartungs-Router: Container/Toolbox aktualisieren.

Der konkrete Befehl steckt in MC_UPDATE_CMD (z.B. kyuz0 refresh-Skript) und
laeuft als Hintergrund-Job mit Live-Log. Spaeter wandert hier ggf. mehr
Server-Wartung hinein (siehe Roadmap: Server-Management).
"""

import shlex
import subprocess
import asyncio

from fastapi import APIRouter, Depends, HTTPException, WebSocket, WebSocketDisconnect
from pydantic import BaseModel

from auth import auth
from config import UPDATE_CMD
from jobengine import start_job

router = APIRouter(prefix="/api", dependencies=[Depends(auth)])

class PwdReq(BaseModel):
    password: str

@router.post("/update")
def update():
    if not UPDATE_CMD:
        raise HTTPException(400, "Kein Update-Befehl gesetzt (MC_UPDATE_CMD).")
    job_id = start_job(shlex.split(UPDATE_CMD), "update containers")
    return {"job_id": job_id}

@router.post("/os-update")
def os_update(req: PwdReq):
    cmd = f"echo {shlex.quote(req.password)} | sudo -S bash -c 'apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y'"
    job_id = start_job(["bash", "-c", cmd], "os update")
    return {"job_id": job_id}

@router.post("/service/{name}/restart")
def service_restart(name: str):
    if name not in ("llama-swap", "mission-control"):
        raise HTTPException(400, "Dienst nicht erlaubt.")
    subprocess.Popen(["sudo", "systemctl", "restart", name])
    return {"ok": True, "note": f"Restart {name} getriggert."}

@router.post("/reboot")
def reboot(req: PwdReq):
    cmd = f"echo {shlex.quote(req.password)} | sudo -S reboot"
    subprocess.Popen(["bash", "-c", cmd])
    return {"ok": True, "note": "Reboot getriggert."}

@router.websocket("/logs/{service}")
async def stream_logs(websocket: WebSocket, service: str):
    await websocket.accept()
    if service not in ("llama-swap", "mission-control"):
        await websocket.close(code=1008)
        return
        
    process = await asyncio.create_subprocess_exec(
        "sudo", "journalctl", "-u", service, "-n", "100", "-f",
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE
    )
    try:
        while True:
            line = await process.stdout.readline()
            if not line:
                break
            await websocket.send_text(line.decode("utf-8", errors="replace"))
    except WebSocketDisconnect:
        pass
    finally:
        try:
            process.terminate()
        except OSError:
            pass
