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_pay_rates():
    start_time = time()

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

    return response({
        "payRates": pay_rates
    }, start_time=start_time)


@router.post("/create", tags=[""])
async def create_pay_rate(params: dict):
    start_time = time()
    data = params["data"]
    data["id"] = await mongo.get_next_id(mongo.pay_rates_collection)

    await mongo.pay_rates_collection.insert_one(data)
    return response({
        "message": "Тариф успешно создан",
        "ok": True
    }, start_time=start_time)


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

    await mongo.pay_rates_collection.update_one({"id": data["id"]}, {"$set": data})
    return response({
        "message": "Тариф успешно обновлен",
        "ok": True
    }, start_time=start_time)


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

    await mongo.pay_rates_collection.delete_one({"id": params["payRateId"]})
    return response({
        "message": "Тариф успешно удален",
        "ok": True
    }, start_time=start_time)
