# coding:utf-8

from fastapi import APIRouter, Depends
from app.api import Response, get_current_user
from app.models.base_model import get_db
from app.models.label_model import LabelData, LabelModel, SignLabelData
from app.models.user_model import UserModel
from app.service.label import create_label_service, label_list_service, delete_role_service, sign_label_service

label_router = APIRouter()

@label_router.get("/list", response_model=Response)
async def get_label_list(keyword="", labelType=1, objectId="",current_user: UserModel = Depends(get_current_user),
                          db=Depends(get_db)):

    return Response(code=200, msg="", data=await label_list_service(db, keyword, labelType, objectId))


@label_router.post("/add_label", response_model=Response)
async def add_label_api(label: LabelData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not label.labelName:
        return Response(code=400, msg="The labelName cannot be empty!")
    db_role = db.query(LabelModel).filter(LabelModel.name == label.labelName).first()
    if db_role:
        return Response(code=400, msg="label already created")
    is_create = await create_label_service(db, label.labelName, label.labelType, current_user.id)
    if not is_create:
        return Response(code=500, msg="label create failure", data={})
    return Response(code=200, msg="label create successfully", data={"roleName": label.labelName})


@label_router.delete("/delete_label", response_model=Response)
async def delete_label_api(labelId: int, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_delete = await delete_role_service(db, labelId)
    if not is_delete:
        return Response(code=500, msg="label delete failure", data={})
    return Response(code=200, msg="label delete successfully", data={})


@label_router.post("/sign_label", response_model=Response)
async def sign_label_api(sign: SignLabelData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_add = await sign_label_service(db, sign.objectId, sign.labelIdList)
    if not is_add:
        return Response(code=500, msg="label add failure", data={})
    return Response(code=200, msg="label sign add successfully", data={})