# 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.role_model import RoleData, RoleModel, RoleStatus, RoleEdit
from app.models.user import PageParameter
from app.models.user_model import UserModel
from app.service.role import role_list, create_role, delete_role_data, edit_role_status, edit_role_resource

role_router = APIRouter()

@role_router.get("/list", response_model=Response)
async def user_group_list(current:int, pageSize:int, keyword="", current_user: UserModel = Depends(get_current_user),
                          db=Depends(get_db)):

    return Response(code=200, msg="", data=await role_list(db, pageSize, current, keyword, current_user.permission, current_user.id))


@role_router.post("/add_role", response_model=Response)
async def add_role(role: RoleData, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not role.roleName:
        return Response(code=400, msg="The roleName cannot be empty!")
    db_role = db.query(RoleModel).filter(RoleModel.name == role.roleName).first()
    if db_role:
        return Response(code=200, msg="role already created")
    is_create = await create_role(db, role.roleName, role.remark, role.roleKey, role.dataScope,current_user.id)
    if not is_create:
        return Response(code=500, msg="role create failure", data={})
    return Response(code=200, msg="role create successfully", data={"roleName": role.roleName})

@role_router.delete("/delete_role/{roleId}", response_model=Response)
async def delete_group(roleId: str, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    db_role = db.query(RoleModel).filter(RoleModel.id == roleId).first()
    if not db_role:
        return Response(code=200, msg="role does not exist")
    if db_role.role_type ==2:
        return Response(code=400, msg="默认角色不允许删除!")
    is_edit = await delete_role_data(db, roleId)
    if not is_edit:
        return Response(code=500, msg="role delete failure", data={})
    return Response(code=200, msg="role delete successfully", data={})


@role_router.put("/change_status", response_model=Response)
async def edit_group_status(role: RoleStatus, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if role.status not in ["0", "1"]:
        return Response(code=400, msg="The status cannot be {}!".format(role.status))
    db_role = db.query(RoleModel).filter(RoleModel.id == role.roleId).first()
    if not db_role:
        return Response(code=200, msg="role does not exist")
    is_edit = await edit_role_status(db, role.status,role.roleId)
    if not is_edit:
        return Response(code=500, msg="role status edit failure", data={})
    return Response(code=200, msg="role status edit successfully", data={})

@role_router.put("/edit_role", response_model=Response)
async def edit_role_data(role: RoleEdit, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if role.editType == 1:
        if not role.roleName:
            return Response(code=400, msg="The roleName cannot be empty!")
        db_role = db.query(RoleModel).filter(RoleModel.id == role.roleId).first()
        if not db_role:
            return Response(code=200, msg="role does not exist")

        db_role = db.query(RoleModel).filter(RoleModel.name == role.roleName).first()
        if db_role and db_role.id != role.roleId:
            return Response(code=200, msg="role already created")
    is_edit = await edit_role_resource(db, role.roleId,role.roleName, role.remark, role.roleKey, role.dataScope, role.resources, role.editType, role.depts)
    if not is_edit:
        return Response(code=500, msg="role edit failure", data={})
    return Response(code=200, msg="role edit successfully", data={})