"""
Shortcut service routes for Memos-style backend.
"""

from typing import List

from fastapi import APIRouter, Body, Depends, HTTPException

from common.memos_core.schemas import (
    ShortcutCreate,
    ShortcutUpdate,
    ShortcutResponse,
    ListShortcutsResponse,
)
from common.memos_core.services import get_shortcut_service


router = APIRouter(prefix="/shortcuts")


@router.get("", summary="List shortcuts", response_model=List[ShortcutResponse])
async def list_shortcuts(shortcut_service=Depends(get_shortcut_service)):
    try:
        return await shortcut_service.list_shortcuts()
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@router.post("", summary="List shortcuts (Connect compatibility)", response_model=ListShortcutsResponse)
async def list_shortcuts_connect_compat(
    payload: dict = Body(default_factory=dict),  # noqa: B008
    shortcut_service=Depends(get_shortcut_service),
):
    # Connect RPC ListShortcuts is proxied as POST /api/v1/shortcuts in dev.
    # Ignore payload (parent, pagination...) and return empty list for now.
    try:
        _ = payload
        shortcuts = await shortcut_service.list_shortcuts()
        return ListShortcutsResponse(shortcuts=shortcuts)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@router.post("", summary="Create shortcut", response_model=ShortcutResponse)
async def create_shortcut(
    payload: ShortcutCreate,
    shortcut_service=Depends(get_shortcut_service),
):
    try:
        return await shortcut_service.create_shortcut(payload)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.patch("/{shortcut_id}", summary="Update shortcut", response_model=ShortcutResponse)
async def update_shortcut(
    shortcut_id: int,
    payload: ShortcutUpdate,
    shortcut_service=Depends(get_shortcut_service),
):
    try:
        return await shortcut_service.update_shortcut(shortcut_id, payload)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.delete("/{shortcut_id}", summary="Delete shortcut")
async def delete_shortcut(
    shortcut_id: int,
    shortcut_service=Depends(get_shortcut_service),
):
    try:
        await shortcut_service.delete_shortcut(shortcut_id)
        return {"status": "success"}
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=400, detail=str(exc)) from exc


