import json
import uuid

from typing import List
from fastapi import Depends, APIRouter, File, UploadFile
from sqlalchemy.orm import Session
from starlette.responses import StreamingResponse, Response
from app.api import get_current_user, get_api_key
from app.config.const import smart_message_error, http_400, http_500, http_200, complex_dialog_chat, \
    complex_knowledge_chat_deep, complex_knowledge_chat
from app.models import UserModel
from app.models.base_model import get_db
from app.models.v2.chat import RetrievalRequest, ChatDataRequest, ComplexChatDao, SetModelRequest
from app.models.v2.session_model import ChatData
from app.service.v2.chat import service_chat_dialog, get_chat_info, service_chat_basic, \
    service_chat_workflow, service_chat_parameters, service_chat_sessions, service_chat_upload, \
    service_chat_sessions_list, service_chat_session_log, service_chunk_retrieval, service_complex_chat, \
    service_complex_upload, service_complex_model, service_get_complex_model

chat_router_v2 = APIRouter()

# 对话
@chat_router_v2.post("/chat/{chatId}/completions")
async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user),db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    chat_info = await get_chat_info(db, chatId)
    if not chat_info:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: parameter exception", "status": http_400})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")
    session_id = dialog.sessionId
    if not dialog.query:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: question cannot be empty.", "status": http_400})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")
    if not session_id:
        session = await service_chat_sessions(db, chatId, dialog.query)
        # print(session)
        if not session or session.get("code") != 0:
            error_msg = json.dumps(
                {"message": smart_message_error, "error": "\n**ERROR**: chat agent error", "status": http_500})
            return StreamingResponse(f"data: {error_msg}\n\n",
                                     media_type="text/event-stream")
        session_id = session.get("data", {}).get("id")
    return StreamingResponse(service_chat_dialog(db, chatId, dialog.query, session_id, current_user.id, chat_info.mode, chat_info.get_kb_ids()),
                             media_type="text/event-stream")

@chat_router_v2.post("/agent/{chatId}/completions")
async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user),db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    chat_info = await get_chat_info(db, chatId)
    if not chat_info:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: parameter exception", "status": http_400})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")
    session_id = dialog.sessionId
    if not session_id:
        session_id = str(uuid.uuid4()).replace("-", "")
    return StreamingResponse(service_chat_workflow(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
                             media_type="text/event-stream")


@chat_router_v2.post("/workflow/{chatId}/completions")
async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    chat_info = await get_chat_info(db, chatId)
    if not chat_info:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: parameter exception", "status": http_400})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")
    session_id = dialog.sessionId
    if not session_id:
        session_id = str(uuid.uuid4()).replace("-", "")
    return StreamingResponse(service_chat_workflow(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
                             media_type="text/event-stream")


@chat_router_v2.post("/develop/{chatId}/completions")
async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    chat_info = await get_chat_info(db, chatId)
    if not chat_info:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: parameter exception", "status": http_400})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")
    session_id = dialog.sessionId
    if not session_id:
        session_id = str(uuid.uuid4()).replace("-", "")


    return StreamingResponse(service_chat_basic(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
                             media_type="text/event-stream")



@chat_router_v2.get("/chat/{chatId}/parameters")
async def api_chat_parameters(chatId:str, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    data = await service_chat_parameters(db, chatId, current_user.id)
    if not data:
        status_code = http_400
        data = json.dumps({"code": http_400})
    return Response(data, media_type="application/json", status_code=status_code)


@chat_router_v2.post("/{chatId}/upload")
async def api_chat_upload(chatId:str, file: List[UploadFile] = File(...), current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    data = await service_chat_upload(db, chatId, file, current_user.id)
    if not data:
        status_code = http_400
        data = "{}"
    return Response(data, media_type="application/json", status_code=status_code)



@chat_router_v2.get("/chat/sessions")
async def api_chat_sessions(chatId:str, current:int=1, pageSize:int=100, keyword:str="", current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    data = await service_chat_sessions_list(db, chatId, current, pageSize, current_user.id, keyword)
    return Response(data, media_type="application/json", status_code=http_200)


@chat_router_v2.get("/chat/session_log")
async def api_chat_sessions(sessionId:str, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    data = await service_chat_session_log(db, sessionId)
    return Response(data, media_type="application/json", status_code=http_200)


@chat_router_v2.post("/retrieval")
async def retrieve_chunks(request_data: RetrievalRequest, api_key: str = Depends(get_api_key)):
    records = await service_chunk_retrieval(request_data.query, request_data.knowledge_id, request_data.retrieval_setting.top_k, request_data.retrieval_setting.score_threshold, api_key)
    return {"records": records}


@chat_router_v2.post("/complex/chat/completions")
async def api_complex_chat_completions(chat: ChatDataRequest, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    # chat_mode = chat.chatMode
    if chat.isDeep == 2 and chat.chatMode == complex_knowledge_chat:
        chat.chatMode = complex_knowledge_chat_deep
    complex_chat = await ComplexChatDao(db).get_complex_chat_by_mode(chat.chatMode)
    if complex_chat:
        if not chat.sessionId:
            chat.sessionId = str(uuid.uuid4()).replace("-", "")
        return StreamingResponse(service_complex_chat(db, complex_chat.id, complex_chat.mode, current_user.id, chat),
                                 media_type="text/event-stream")
    else:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: 网络异常，无法生成对话结果！", "status": http_500})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")


@chat_router_v2.post("/complex/upload/{chatMode}")
async def api_complex_upload(chatMode:int, file: List[UploadFile] = File(...), current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    complex_chat = await ComplexChatDao(db).get_complex_chat_by_mode(chatMode)
    if complex_chat:
        data = await service_complex_upload(db, complex_chat.id, file, current_user.id)
        if not data:
            status_code = http_400
            data = "{}"
    else:
        status_code = http_500
        data = "{}"
    return Response(data, media_type="application/json", status_code=status_code)


@chat_router_v2.put("/complex/model")
async def api_complex_model(chatModel:SetModelRequest, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    data = await service_complex_model(db, chatModel.chatType, chatModel.modelType, chatModel.modelName, chatModel.modelProvider)
    if data:
        status_code = http_500
    return Response(data, media_type="application/json", status_code=status_code)


@chat_router_v2.get("/complex/model")
async def api_get_complex_model(current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    data = await service_get_complex_model(db)
    if not data:
        status_code = http_500
    return Response(data, media_type="application/json", status_code=status_code)



