"""WESP-shaped misc endpoints: notifications, updates (/api/notifications, /api/updates/*)."""

from __future__ import annotations

from datetime import date

from fastapi import APIRouter, Depends
from pydantic import BaseModel

from app.core.dependencies import get_current_user
from app.modules.users.models import User

router = APIRouter(tags=["zootech-wesp-misc"])


def _empty_notifications(*, summary: bool = False) -> dict:
    today = date.today().isoformat()
    if summary:
        return {"items": [], "unreadCount": 0}
    return {
        "items": [],
        "unreadCount": 0,
        "date": today,
        "prevDate": None,
        "hasMore": False,
    }


@router.get("/notifications")
def wesp_notifications_list(
    summary: str | None = None,
    _user: User = Depends(get_current_user),
):
    if summary in ("1", "true", "yes"):
        return _empty_notifications(summary=True)
    return _empty_notifications()


@router.post("/notifications")
def wesp_notifications_create(_user: User = Depends(get_current_user)):
    return {
        "id": "orchestrator-stub",
        "title": "",
        "detail": "",
        "kind": "info",
        "category": "general",
        "read": False,
    }


@router.patch("/notifications/read-all")
def wesp_notifications_read_all(_user: User = Depends(get_current_user)):
    return {"success": True, "marked": 0}


@router.patch("/notifications/{notification_id}/read")
def wesp_notifications_mark_read(notification_id: str, _user: User = Depends(get_current_user)):
    return {"id": notification_id, "read": True}


@router.get("/updates/status")
def wesp_updates_status(_user: User = Depends(get_current_user)):
    return {
        "initialized": False,
        "check_enabled": False,
        "current_version": "1.0.0-orchestrator",
        "update_available": False,
        "pending_version": None,
        "pending_name": None,
        "pending_body": None,
        "published_at": None,
        "last_check_at": None,
        "is_running": False,
        "is_updating": False,
        "update_progress": None,
        "last_update_state": {"status": "idle", "note": "orchestrator deploy"},
        "gitea_configured": False,
        "restart_configured": False,
        "gitea_url": None,
        "gitea_repo": None,
    }


class UpdatesInstallIn(BaseModel):
    version: str | None = None


@router.post("/updates/install")
def wesp_updates_install(_payload: UpdatesInstallIn, _user: User = Depends(get_current_user)):
    return {
        "status": "error",
        "message": "Автообновление недоступно на orchestrator — используйте деплой Docker.",
    }


@router.get("/updates/check")
def wesp_updates_check(_user: User = Depends(get_current_user)):
    return {
        "update_available": False,
        "message": "Проверка обновлений недоступна на orchestrator.",
    }
