from fastapi import APIRouter, Depends, Request
from db.handlers.access import AccessLevelHandler
from db.handlers.user import UserHandler
from utils import requestDict, logger, render


router = APIRouter()


@router.get("/", name="userInfo", summary="Получение информации о пользователе")
async def get_user():
    return


@router.post("/")
async def create_user():
    return


@router.get("/login", name="Authentication", summary="Авторизация пользователя")
async def authenticationPage(request: Request):
    return await render(request)


@router.post("/login")
async def authentication(
    request_data: dict = Depends(requestDict),
):
    resultData = {"status": "error", "user": {}, "access": {}}
    login = request_data.get("body").get("login", None)
    password = request_data.get("body").get("password", None)
    if not login or not password:
        logger.error("Не указан логин или пароль")
        return resultData
    userData = await UserHandler.auth(login, password)
    if not userData:
        return resultData
    accessData = await AccessLevelHandler.get(userData["access_level_id"])
    if not accessData:
        return resultData
    resultData["status"] = "ok"
    resultData["user"] = userData
    resultData["access"] = accessData
    return resultData
