import random
from orderDistributor import OrderDistributor


# This dict represents the costs of making each product in each group
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
    }
}
WAITERS = ["Jan", "Pol", "Martina", "Sandra", "Dani", "Marc", "Ivet"]



class CommandManager:
    def __init__(self):
        self.product_costs = PRODUCT_COSTS
        self.waiters = WAITERS
        self.orders = []
        self.distributor = OrderDistributor(self.product_costs, self.waiters)


    def run(self):
        self.orders = self.create_multiple_orders()
        for i, order in enumerate(self.orders):
            print(f"Order {i+1}: {order}")
        return self.distributor.distribute_orders(self.orders)


    def create_single_order(self):
        """
        Creates a single order with randomly selected products and units (1-4).
        """
        available_products = list(
            (prod, cost)
            for group in self.product_costs.values()
            for prod, cost in group.items()
        )
        num_unique_products = random.choices([2, 3, 4, 5, 6, 7, 8], weights=[20, 30, 30, 10, 5, 3, 2])[0]
        selected_products = random.sample(available_products, k=num_unique_products)
        order = {}
        total_cost = 0
        for product, cost in selected_products:
            quantity = random.choices([1, 2, 3, 4, 5, 6], weights=[45, 25, 15, 8, 5, 2])[0]
            order[product] = quantity
            total_cost += cost * quantity
        return order


    def create_multiple_orders(self):
        """
        Generates a list of orders. The number of orders is random (between 2 and 10).
        Each element of the list is an order generated by create_single_order.
        """
        num_orders = random.randint(5, 20)
        order_list = []
        print(f"Generating {num_orders} orders...")
        for _ in range(num_orders):
            order = self.create_single_order()
            order_list.append(order)
        return order_list
    
