# coding:utf-8

from fastapi import APIRouter, Depends
from app.api import Response, get_current_user, ResponseList
from app.models.dialog_model import dialogData, dialogDataUpdate, dialogList
from app.models.base_model import get_db
from app.models.user_model import UserModel
from app.service.dialog import get_dialog_list, create_dialog_service, update_dialog_status_service, \
    delete_dialog_service, update_dialog_icon_service, get_dialog_manage_list, sync_dialog_service

dialog_router = APIRouter()


@dialog_router.get("/list", response_model=Response)
async def dialog_list(current: int,
                      pageSize: int,
                      keyword: str = "",
                      label: int = 0,
                      status: str ="",
                      mode: int = 0,
                      current_user: UserModel = Depends(get_current_user),
                      db=Depends(get_db)):
    if current and not pageSize:
        return Response(code=400, msg="缺少参数")
    if status and status not in ["0", "1"]:
        return Response(code=400, msg="Parameter status exception")
    return Response(code=200, msg="", data=await get_dialog_list(db, current_user.id, keyword, label, status, pageSize, current, mode))


@dialog_router.post("/create", response_model=Response)
async def create_dialog_api(dialog: dialogData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_create = await create_dialog_service(db, dialog.id, dialog.name, dialog.description, dialog.icon, dialog.dialogType, dialog.mode,current_user.id)
    if not is_create:
        return Response(code=500, msg="role create failure", data={})
    return Response(code=200, msg="role create success", data={})


@dialog_router.get("/update", response_model=Response)
async def change_dialog_data(dialog: dialogDataUpdate, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    # is_create = await create_dialog_service(db, dialog.id, dialog.name, dialog.description, dialog.icon, dialog.dialogType)
    # if not is_create:
    #     return Response(code=500, msg="role create failure", data={})
    return Response(code=200, msg="role create success", data={})


@dialog_router.put("/status", response_model=Response)
async def change_dialog_status(dialog: dialogDataUpdate, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if dialog.status not in ["0", "1"]:
        return Response(code=400, msg="invalid parameter value", data={})
    is_create = await update_dialog_status_service(db, dialog.id, dialog.status, current_user.id)
    if not is_create:
        return Response(code=500, msg="dialog update failure", data={})
    return Response(code=200, msg="dialog update success", data={})


@dialog_router.get("/delete", response_model=Response)
async def sync_knowledge_api(dialogId: str, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_delete = await delete_dialog_service(db, dialogId)
    if not is_delete:
        return Response(code=500, msg="knowledge delete failure", data={})
    return Response(code=200, msg="knowledge delete success", data={})


@dialog_router.put("/update_icon", response_model=Response)
async def change_dialog_icon(dialog: dialogDataUpdate, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_create = await update_dialog_icon_service(db, dialog.id, dialog.icon, dialog.name, dialog.description)
    if not is_create:
        return Response(code=500, msg="dialog update failure", data={})
    return Response(code=200, msg="dialog update success", data={})


@dialog_router.post("/dialog_list", response_model=Response)
async def dialog_list_api(dialog:dialogList,
                      current_user: UserModel = Depends(get_current_user),
                      db=Depends(get_db)):
    return Response(code=200, msg="", data=await get_dialog_manage_list(db, current_user.id, dialog.keyword, dialog.label, dialog.status, dialog.pageSize, dialog.current, dialog.mode))


@dialog_router.get("/sync", response_model=Response)
async def sync_dialog_api(dialogId: str, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_create = await sync_dialog_service(db, dialogId)
    if not is_create:
        return Response(code=500, msg="dialog update failure", data={})
    return Response(code=200, msg="dialog update success", data={})