from __future__ import annotations

from fastapi import APIRouter, Depends, Header

from backend.app.api.schemas import (
    RemoteClientHeartbeatRequest,
    RemoteClientItem,
    RemoteClientListResponse,
    RemoteClientRegisterRequest,
)
from backend.app.dependencies import get_remote_client_service
from backend.app.services.remote_client_service import RemoteClientService

router = APIRouter(prefix="/clients")


@router.get("", response_model=RemoteClientListResponse)
async def list_clients(
    service: RemoteClientService = Depends(get_remote_client_service),
) -> RemoteClientListResponse:
    return service.list_clients()


@router.post("/register", response_model=RemoteClientItem)
async def register_client(
    request: RemoteClientRegisterRequest,
    authorization: str | None = Header(default=None),
    service: RemoteClientService = Depends(get_remote_client_service),
) -> RemoteClientItem:
    return service.register_client(authorization=authorization, request=request)


@router.post("/heartbeat", response_model=RemoteClientItem)
async def heartbeat(
    request: RemoteClientHeartbeatRequest,
    authorization: str | None = Header(default=None),
    service: RemoteClientService = Depends(get_remote_client_service),
) -> RemoteClientItem:
    return service.record_heartbeat(authorization=authorization, request=request)
