from time import time

from fastapi import APIRouter

from app import mongo
from app.utils.response_util import response

router = APIRouter()


@router.get("/get-all", tags=[""])
async def get_all():
    start_time = time()

    positions = await mongo.positions_collection.find({}, {
        "_id": False
    }).sort("id", mongo.asc).to_list()

    return response({
        "positions": positions
    }, start_time=start_time)


@router.post("/create", tags=[""])
async def create_position(params: dict):
    start_time = time()

    data = params["data"]
    data["id"] = await mongo.get_next_id(mongo.positions_collection)

    await mongo.positions_collection.insert_one(data)
    return response({
        "message": "Должность успешно создана",
        "ok": True
    }, start_time=start_time)


@router.post("/delete", tags=[""])
async def delete_position(params: dict):
    start_time = time()

    await mongo.positions_collection.delete_one({"key": params["positionKey"]})
    return response({
        "message": "Должность успешно удалена",
        "ok": True
    }, start_time=start_time)
