"""API routes for the auto-update system."""

from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse

from wled_controller.api.auth import AuthRequired
from wled_controller.api.dependencies import get_update_service
from wled_controller.api.schemas.update import (
    DismissRequest,
    UpdateSettingsRequest,
    UpdateSettingsResponse,
    UpdateStatusResponse,
)
from wled_controller.core.update.update_service import UpdateService
from wled_controller.utils import get_logger

logger = get_logger(__name__)

router = APIRouter(prefix="/api/v1/system/update", tags=["update"])


@router.get("/status", response_model=UpdateStatusResponse)
async def get_update_status(
    _: AuthRequired,
    service: UpdateService = Depends(get_update_service),
):
    return service.get_status()


@router.post("/check", response_model=UpdateStatusResponse)
async def check_for_updates(
    _: AuthRequired,
    service: UpdateService = Depends(get_update_service),
):
    return await service.check_now()


@router.post("/dismiss")
async def dismiss_update(
    _: AuthRequired,
    body: DismissRequest,
    service: UpdateService = Depends(get_update_service),
):
    service.dismiss(body.version)
    return {"ok": True}


@router.post("/apply")
async def apply_update(
    _: AuthRequired,
    service: UpdateService = Depends(get_update_service),
):
    """Download (if needed) and apply the available update."""
    status = service.get_status()
    if not status["has_update"]:
        return JSONResponse(status_code=400, content={"detail": "No update available"})
    if not status["can_auto_update"]:
        return JSONResponse(
            status_code=400,
            content={"detail": f"Auto-update not supported for install type: {status['install_type']}"},
        )
    try:
        await service.apply_update()
        return {"ok": True, "message": "Update applied, server shutting down"}
    except Exception as exc:
        logger.error("Failed to apply update: %s", exc, exc_info=True)
        return JSONResponse(status_code=500, content={"detail": "Internal server error"})


@router.get("/settings", response_model=UpdateSettingsResponse)
async def get_update_settings(
    _: AuthRequired,
    service: UpdateService = Depends(get_update_service),
):
    return service.get_settings()


@router.put("/settings", response_model=UpdateSettingsResponse)
async def update_update_settings(
    _: AuthRequired,
    body: UpdateSettingsRequest,
    service: UpdateService = Depends(get_update_service),
):
    return await service.update_settings(
        enabled=body.enabled,
        check_interval_hours=body.check_interval_hours,
        include_prerelease=body.include_prerelease,
    )
