#!/usr/bin/env python

import typer
import asyncio
from functools import wraps

from faker import Faker
from tortoise import Tortoise

from app.config.db import TORTOISE_ORM
from app.items.models import Item


# @see https://github.com/pallets/click/issues/85#issuecomment-503464628
def coroutine(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return asyncio.run(f(*args, **kwargs))

    return wrapper


@coroutine
async def generatefakedata():
    await Tortoise.init(TORTOISE_ORM)

    fake = Faker()

    for i in range(100):
        name = fake.company()
        catch_phrase = fake.catch_phrase()
        credit_card_number = fake.credit_card_number()

        item = await Item.create(name=name, catch_phrase=catch_phrase, credit_card_number=credit_card_number)

        typer.echo(f"Item \"{item.name}\" created!")


if __name__ == "__main__":
    typer.run(generatefakedata)
