import os

from fastapi import APIRouter, HTTPException

router = APIRouter()

NOMADNET_CONTAINER = os.environ.get("NOMADNET_CONTAINER", "nomadnet")


@router.post("/restart")
async def restart_nomadnet():
    try:
        import docker

        client = docker.from_env()
        container = client.containers.get(NOMADNET_CONTAINER)
        container.restart()
        return {"status": "restarted", "container": NOMADNET_CONTAINER}
    except docker.errors.NotFound:
        raise HTTPException(
            status_code=404,
            detail=f"Container '{NOMADNET_CONTAINER}' not found",
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
