import requests

API_URL = "http://127.0.0.1:8000"

PRODUCT_COSTS = {
    "fridges": {
        "cocacola": 1,
        "fanta": 1,
        "lipton": 1,
        "aquarius": 2,
        "magners": 2,
        "kopparberg": 2,
        "schweppes 0": 2,
    },
    "alone_make_products": {
        "caipirinha" : 9,
        "espresso_martini" : 7,
        "long_island_iced_tea": 6,
        "guiness_shot": 7
    },
    "indoor_cocktails": {
        "moscow_mule": 6,
        "whiskey_sour": 6,
        "capitan_morgan_sour": 6,
        "strawberry_daikiri": 6
    },
    "mojitos": {
        "mojito": 7,
        "strawberry_mojito": 7,
        "passionfruit_mojito": 7,
        "captain_morgan_mojito": 8,
        "no_alcohol_mojito": 5
    },
    "bar_cocktails": {
        "pornstar_martini": 5,
        "aperol": 4,
        "gintonic": 3,
        "tequila_sunrise": 4
    },
    "outdoor_cocktails": {
        "pina_colada": 3,
        "blue_lagoon": 3,
        "sex_on_the_beach": 3,
        "piña_colada_sin": 3
    },
    "coffee_machine": {
        "espresso": 2,
        "latte_macchiato": 3,
        "latte": 3,
        "cappuccino": 4,
        "tea": 2
    },
    "sangrias": {
        "sangria_1p": 4,
        "sangria_2p": 5,
        "sangria_3p": 6,
        "sangria_4p": 7,
        "sangria_5p": 8,
    },
    "combinats": {
        "cubata": 3,
        "gin_lemon": 3,
        "rum_lemon": 3,
        "vodka_lemon": 3,
        "redbull_vodka": 3
    }
}

def create_groups_and_products():
    # 1. Crear grupos
    group_name_to_id = {}
    for group_name in PRODUCT_COSTS.keys():
        resp = requests.post(f"{API_URL}/groups/", json={"name": group_name})
        if resp.status_code == 200 or resp.status_code == 201:
            group_id = resp.json()["id"]
            group_name_to_id[group_name] = group_id
            print(f"Grupo creado: {group_name} (id={group_id})")
        elif resp.status_code == 400 and "already exists" in resp.text:
            # Si ya existe, obtener el id
            groups = requests.get(f"{API_URL}/groups/").json()
            for g in groups:
                if g["name"] == group_name:
                    group_name_to_id[group_name] = g["id"]
                    break
            print(f"Grupo ya existía: {group_name}")
        else:
            print(f"Error creando grupo {group_name}: {resp.status_code} {resp.text}")
    # 2. Crear productos
    for group_name, products in PRODUCT_COSTS.items():
        group_id = group_name_to_id[group_name]
        for name, cost in products.items():
            # Precio inventado: cost + random entre 1 y 4
            import random
            price = float(cost + random.randint(1, 4))
            product_data = {
                "name": name,
                "price": price,
                "cost": float(cost),
                "group_id": group_id
            }
            resp = requests.post(f"{API_URL}/products/", json=product_data)
            if resp.status_code == 200 or resp.status_code == 201:
                print(f"Producto creado: {name} (grupo: {group_name})")
            elif resp.status_code == 400 and "already exists" in resp.text:
                print(f"Producto ya existía: {name}")
            else:
                print(f"Error creando producto {name}: {resp.status_code} {resp.text}")

if __name__ == "__main__":
    create_groups_and_products()
