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

Chỉ định nghĩa API, logic sẽ nằm ở memos_core.services.
"""

from fastapi import APIRouter, Depends, HTTPException

from memos_core.schemas import InstanceInfo, InstanceSetting, InstanceUpdate
from memos_core.services import get_instance_service


router = APIRouter(prefix="/instance")


@router.get("", summary="Get instance info", response_model=InstanceInfo)
@router.post("", summary="Get instance info (Connect compatibility)", response_model=InstanceInfo)
async def get_instance(instance_service=Depends(get_instance_service)):
    try:
        return await instance_service.get_instance_info()
    except Exception as exc:  # pragma: no cover - placeholder
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@router.patch("", summary="Update instance basic settings", response_model=InstanceInfo)
async def update_instance(
    payload: InstanceUpdate,
    instance_service=Depends(get_instance_service),
):
    try:
        return await instance_service.update_instance_info(payload)
    except Exception as exc:  # pragma: no cover - placeholder
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@router.post("/setting", summary="Get instance setting (Connect compatibility)", response_model=InstanceSetting)
async def get_instance_setting(instance_service=Depends(get_instance_service)):
    try:
        return await instance_service.get_instance_setting()
    except Exception as exc:  # pragma: no cover - placeholder
        raise HTTPException(status_code=500, detail=str(exc)) from exc


