import uuid
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.domain.models import DocumentCreateRequest, DocumentCreatedResponse
from app.service import documents_service
from app.api.dependencies import DBSessionDep, CitizenDep

router = APIRouter(prefix="/documents", tags=["documents"])

@router.post("", response_model=DocumentCreatedResponse, status_code=status.HTTP_201_CREATED)
async def create_document(
    payload: DocumentCreateRequest,
    session: AsyncSession = DBSessionDep,
    citizen = CitizenDep,
):
    return await documents_service.create_document(payload, session)

@router.get("/{document_id}", response_model=DocumentCreatedResponse)
async def get_document(
    document_id: str,
    session: AsyncSession = DBSessionDep,
    citizen = CitizenDep,
):
    try:
        doc_uuid = uuid.UUID(document_id)
    except ValueError:
        raise HTTPException(status_code=404, detail="Document not found")

    doc = await documents_service.get_document(doc_uuid, session)
    if not doc:
        raise HTTPException(status_code=404, detail="Document not found")

    return DocumentCreatedResponse(
        id=str(doc.id),
        status=doc.status,
        assignedUnit=doc.assigned_unit,
    )
