from __future__ import annotations

from fastapi import APIRouter

from app.core.command_runner import CommandRunner
from app.core.service_manager import ServiceManager
from app.models.schemas import ServiceRequest
from app.storage.repository import Repository


router = APIRouter(prefix="/services", tags=["services"])


def _manager() -> ServiceManager:
    return ServiceManager(CommandRunner(Repository()))


@router.post("/start")
def start_service(request: ServiceRequest) -> dict[str, str]:
    _manager().start_service(request.service_name)
    return {"serviceName": request.service_name, "status": "started"}


@router.post("/stop")
def stop_service(request: ServiceRequest) -> dict[str, str]:
    _manager().stop_service(request.service_name)
    return {"serviceName": request.service_name, "status": "stopped"}


@router.post("/restart")
def restart_service(request: ServiceRequest) -> dict[str, str]:
    _manager().restart_service(request.service_name)
    return {"serviceName": request.service_name, "status": "restarted"}


@router.get("/{service_name}/status")
def service_status(service_name: str) -> dict[str, object]:
    request = ServiceRequest(serviceName=service_name)
    return _manager().get_service_status(request.service_name)

