import random

from locust import FastHttpUser, task

from learn_sql_model.config import get_config
from learn_sql_model.factories.hero import HeroFactory
from learn_sql_model.models.hero import HeroCreate, HeroUpdate, Heros

config = get_config()


class QuickstartUser(FastHttpUser):
    # wait_time = between(1, 2)
    host = "http://localhost:5000"
    # host = "https://waylonwalker.com"

    def on_start(self):
        self.client.verify = False

    @task(6)
    def get_a_hero(self):
        # heros = Heros.list()
        id = 1
        # id = random.choice(heros.__root__).id

        self.client.get(f"/hero/{id}")

    # @task(2)
    # def get_all_hero(self):
    #     self.client.get("/heros/")

    @task
    def create_hero(self):
        hero = HeroFactory().build()
        hero_create = HeroCreate(**hero.dict()).post()

        self.client.post(
            f"{config.api_client.url}/hero/",
            json=hero_create.dict(),
        )

    @task(3)
    def update_hero(self):
        hero = HeroFactory().build()
        hero_update = HeroUpdate(id=1, name=hero.name)

        self.client.patch(
            "/hero/",
            json=hero_update.dict(exclude_none=True),
        )

    @task
    def delete_hero(self):
        heros = Heros.list()
        id = random.choice(heros.__root__).id
        self.client.delete(
            f"/hero/{id}",
        )
