"""
Identity Provider (IDP) service routes for Memos-style backend.
"""

from typing import List

from fastapi import APIRouter, Depends, HTTPException

from common.memos_core.schemas import (
    IdentityProviderCreate,
    IdentityProviderUpdate,
    IdentityProviderResponse,
)
from common.memos_core.services import get_idp_service


router = APIRouter(prefix="/idp")


@router.get("", summary="List identity providers", response_model=List[IdentityProviderResponse])
async def list_identity_providers(idp_service=Depends(get_idp_service)):
    try:
        return await idp_service.list_identity_providers()
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@router.post("", summary="Create identity provider", response_model=IdentityProviderResponse)
async def create_identity_provider(
    payload: IdentityProviderCreate,
    idp_service=Depends(get_idp_service),
):
    try:
        return await idp_service.create_identity_provider(payload)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.patch("/{idp_id}", summary="Update identity provider", response_model=IdentityProviderResponse)
async def update_identity_provider(
    idp_id: int,
    payload: IdentityProviderUpdate,
    idp_service=Depends(get_idp_service),
):
    try:
        return await idp_service.update_identity_provider(idp_id, payload)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=400, detail=str(exc)) from exc


