"""Documents CRUD router for RAG Database management."""
import logging

from fastapi import APIRouter, HTTPException

from app.models.documents import (
    DocumentInfo,
    ChunkInfo,
    DocumentListResponse,
    DeleteResponse,
)
from app.services.rag import RAGService

logger = logging.getLogger(__name__)
router = APIRouter(tags=["documents"])


@router.get("/documents", response_model=DocumentListResponse)
async def list_documents():
    from app.core.config import get_settings

    settings = get_settings()
    rag = RAGService(settings=settings)

    doc_list, total_docs, total_chunks = rag.list_documents()

    documents = [
        DocumentInfo(
            document_id=d["document_id"],
            filename=d["filename"],
            chunk_count=d["chunk_count"],
            upload_date=d["upload_date"],
        )
        for d in doc_list
    ]

    return DocumentListResponse(
        documents=documents,
        total_documents=total_docs,
        total_chunks=total_chunks,
    )


@router.get("/documents/{document_id}/chunks", response_model=list[ChunkInfo])
async def list_chunks(document_id: str):
    from app.core.config import get_settings

    settings = get_settings()
    rag = RAGService(settings=settings)

    chunks = rag.list_chunks(document_id)

    return [
        ChunkInfo(
            chunk_id=c["chunk_id"],
            chunk_index=c["chunk_index"],
            content_summary=c["content_summary"],
            page_number=c.get("page_number"),
            chunk_file_path=c.get("chunk_file_path"),
        )
        for c in chunks
    ]


@router.delete("/documents/{document_id}", response_model=DeleteResponse)
async def delete_document(document_id: str):
    from app.core.config import get_settings

    settings = get_settings()
    rag = RAGService(settings=settings)

    success, deleted_count = rag.delete_document(document_id)

    if not success:
        raise HTTPException(status_code=404, detail=f"Document not found: {document_id}")

    logger.info("Deleted document %s: %d chunks removed", document_id, deleted_count)

    return DeleteResponse(
        deleted=True,
        message=f"Deleted document {document_id}: {deleted_count} chunks removed",
    )


@router.delete("/chunks/{chunk_id}", response_model=DeleteResponse)
async def delete_chunk(chunk_id: str):
    from app.core.config import get_settings

    settings = get_settings()
    rag = RAGService(settings=settings)

    success = rag.delete_chunk(chunk_id)

    if not success:
        raise HTTPException(status_code=404, detail=f"Chunk not found: {chunk_id}")

    logger.info("Deleted chunk: %s", chunk_id)

    return DeleteResponse(
        deleted=True,
        message=f"Deleted chunk {chunk_id}",
    )
