import json
import uuid
from typing import List

from fastapi import Depends, APIRouter, File, UploadFile
from sqlalchemy.orm import Session
from werkzeug.http import HTTP_STATUS_CODES

from app.api import get_current_user, get_api_key, Response
from app.config.const import dialog_chat, advanced_chat, base_chat, agent_chat, workflow_chat, basic_chat, \
    smart_message_error, http_400, http_500, http_200, complex_mindmap_chat
from app.models import UserModel
from app.models.base_model import get_db
from app.models.v2.chat import RetrievalRequest, ComplexChatDao
from app.models.v2.mindmap import MindmapRequest
from app.models.v2.session_model import ChatData
from app.service.v2.mindmap import service_chat_mindmap, service_message_mindmap_parse

mind_map_router = APIRouter()


@mind_map_router.post("/create", response_model=Response)
async def api_chat_mindmap(mindmap: MindmapRequest, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    complex_chat = await ComplexChatDao(db).get_complex_chat_by_mode(complex_mindmap_chat)
    if complex_chat:
        data = await service_chat_mindmap(db, mindmap.messageId, mindmap.query, complex_chat.id,current_user.id)
        if not data:
            return Response(code=500, msg="create failure", data={})
    else:
        return Response(code=500, msg="网络异常！failure", data={})
    return Response(code=200, msg="create success", data=data)


@mind_map_router.get("/{messageId}/parse", response_model=Response)
async def api_chat_mindmap(messageId: str, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)

    data = await service_message_mindmap_parse(db, messageId, current_user.id)
    if not data:
        return Response(code=500, msg="create failure", data={})
    return Response(code=200, msg="create success", data=data)