from fastapi import APIRouter, Depends
from app.api import Response, pwd_context, get_current_user
from app.models import MenuInfo, MenuStatus
from app.models.base_model import get_db
from app.models.user_model import UserModel
from app.service.resource import get_resource_tree, get_resource_info, delete_resource_info, create_menu, \
    edit_resource_data, edit_resource_status

menu_router = APIRouter()


@menu_router.get("/tree_select", response_model=Response)
async def resource_tree_select(current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    return Response(code=200, msg="", data=await get_resource_tree(db))

@menu_router.get("/{menuId}", response_model=Response)
async def resource_info(menuId, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):

    return Response(code=200, msg="", data=await get_resource_info(db, menuId))


@menu_router.delete("/{menuId}", response_model=Response)
async def delete_resource(menuId, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    msg = await delete_resource_info(db, menuId)
    if msg:
        return Response(code=400, msg=f"{msg}", data={})
    return Response(code=200, msg="menu delete successfully", data={})



@menu_router.post("/add_menu", response_model=Response)
async def resource_add_menu(menu: MenuInfo, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not menu.menuName:
        return Response(code=400, msg="The menuName cannot be empty!")
    if menu.status not in ["0", "1"]:
        return Response(code=400, msg="The status cannot be {}!".format(menu.status))
    is_create = await create_menu(db, menu.menuName, menu.component, menu.path, menu.orderNum, menu.perms, menu.menuType, menu.description, menu.parentId, menu.status,menu.icon)
    if not is_create:
        return Response(code=500, msg="menu create failure", data={})
    return Response(code=200, msg="menu create successfully", data={})


@menu_router.put("/edit_menu", response_model=Response)
async def resource_edit_menu(menu: MenuInfo, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not menu.menuName:
        return Response(code=400, msg="The menuName cannot be empty!")
    if menu.status not in ["0", "1"]:
        return Response(code=400, msg="The status cannot be {}!".format(menu.status))
    is_create = await edit_resource_data(db, menu.menuId, menu.menuName, menu.component, menu.path, menu.orderNum, menu.perms, menu.menuType, menu.description, menu.parentId, menu.status,menu.icon)
    if not is_create:
        return Response(code=500, msg="menu edit failure", data={})
    return Response(code=200, msg="menu edit successfully", data={})


@menu_router.put("/status", response_model=Response)
async def resource_edit_status(menu: MenuStatus, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if menu.status not in ["0", "1"]:
        return Response(code=400, msg="The status cannot be {}!".format(menu.status))
    # is_create = await edit_resource_status(db, menu.menuId, menu.status)
    msg = await edit_resource_status(db, menu.menuId, str(menu.status))
    if msg:
        return Response(code=400, msg=f"{msg}", data={})
    return Response(code=200, msg="menu status update successfully", data={})
