from fastapi import APIRouter, Depends
from app.api import Response, pwd_context, get_current_user
from app.models import DeptList, DeptInfo, DeptParent, DeptStatus
from app.models.base_model import get_db
from app.models.user import PageParameter, UserStatus, UserInfo, LoginData
from app.models.user_model import UserModel
from app.service.organization import get_organization_list, create_dept, edit_dept_data, edit_dept_parent, \
    get_organization_info, delete_organization_info, edit_organization_status

dept_router = APIRouter()


@dept_router.get("/list", response_model=Response)
async def organization_list(deptName="",current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    return Response(code=200, msg="", data=await get_organization_list(db, deptName))


@dept_router.post("/add_dept", response_model=Response)
async def add_dept(dept: DeptInfo, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not dept.deptName:
        return Response(code=400, msg="The deptName cannot be empty!")
    if dept.status not in ["0", "1", 0, 1]:
        return Response(code=400, msg="The status cannot be {}!".format(dept.status))
    is_create = await create_dept(db, dept.deptName, dept.leader, dept.phone, dept.address, dept.status, dept.orderNum, dept.roles, dept.groups, dept.parentId)
    if not is_create:
        return Response(code=500, msg="dept create failure", data={})
    return Response(code=200, msg="dept create successfully", data={})


@dept_router.put("/edit_dept", response_model=Response)
async def edit_dept(dept: DeptInfo, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if not dept.deptName:
        return Response(code=400, msg="The deptName cannot be empty!")
    if dept.status not in ["0", "1"]:
        return Response(code=400, msg="The status cannot be {}!".format(dept.status))
    is_create = await edit_dept_data(db, dept.deptId, dept.deptName, dept.leader, dept.phone, dept.address, dept.status, dept.orderNum, dept.roles, dept.groups, dept.parentId)
    if not is_create:
        return Response(code=500, msg="dept edit failure", data={})
    return Response(code=200, msg="dept edit successfully", data={})

@dept_router.put("/dept_parent", response_model=Response)
async def dept_parent(dept: DeptParent, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    is_create = await edit_dept_parent(db, dept.deptId, dept.parentId, dept.orderNum)
    if not is_create:
        return Response(code=500, msg="dept edit failure", data={})
    return Response(code=200, msg="dept edit successfully", data={})


@dept_router.get("/{deptId}", response_model=Response)
async def dept_info(deptId, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):

    return Response(code=200, msg="", data=await get_organization_info(db, deptId))


@dept_router.delete("/{deptId}", response_model=Response)
async def delete_dept(deptId, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    msg = await delete_organization_info(db, deptId)
    if msg:
        return Response(code=400, msg=f"{msg}", data={})
    return Response(code=200, msg="dept delete successfully", data={})


@dept_router.put("/status", response_model=Response)
async def edit_status_api(dept: DeptStatus, current_user: UserModel = Depends(get_current_user), db=Depends(get_db)):
    if dept.status not in ["0", '1', 0, 1]:
        return Response(code=400, msg="未知状态！", data={})
    msg = await edit_organization_status(db, dept.deptId, str(dept.status))
    if msg:
        return Response(code=400, msg=f"{msg}", data={})
    return Response(code=200, msg="dept status update successfully", data={})
