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

from typing import List

from fastapi import APIRouter, Depends, HTTPException, UploadFile, File

from common.memos_core.schemas import (
    AttachmentResponse,
)
from common.memos_core.services import get_attachment_service


router = APIRouter(prefix="/attachments")


@router.get("", summary="List attachments", response_model=List[AttachmentResponse])
async def list_attachments(attachment_service=Depends(get_attachment_service)):
    try:
        return await attachment_service.list_attachments()
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@router.post("", summary="Upload attachment", response_model=AttachmentResponse)
async def upload_attachment(
    file: UploadFile = File(...),
    attachment_service=Depends(get_attachment_service),
):
    try:
        return await attachment_service.upload_attachment(file)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.get("/{attachment_id}", summary="Get attachment by ID", response_model=AttachmentResponse)
async def get_attachment(attachment_id: str, attachment_service=Depends(get_attachment_service)):
    try:
        return await attachment_service.get_attachment(attachment_id)
    except Exception as exc:  # pragma: no cover
        raise HTTPException(status_code=404, detail=str(exc)) from exc


