"""
pizza_bot_enhanced.py  -  Mario's Pizzeria Kiosk, version 1.1
==============================================================
Everything pizza_bot.py does, plus the optional requirements (BRS FR-14 to FR-21):
  * four topping sets: Classic Meat Supreme, Vegetarian, Supreme (+$3), Chicken (+$3)
  * Tomato or BBQ sauce (+$1)
  * Extra Crispy crust (+$1)
  * every order is appended to orders.log (one line per order)
  * the receipt is saved to receipt.txt so it can be emailed or texted

pizza_bot.py is the school submission and is left exactly as it was.
"""

import json
from datetime import datetime

# ---------- prices (v1.0 prices unchanged) ----------
PRICE_SMALL, PRICE_LARGE = 10, 15
PRICE_GLUTEN_FREE, PRICE_DAIRY_FREE = 3, 2
PRICE_GARLIC_BREAD, PRICE_DELIVERY = 4, 5
PRICE_SUPREME, PRICE_CHICKEN, PRICE_BBQ, PRICE_CRISPY = 3, 3, 1, 1

TOPPINGS = {
    "classic":    ("Classic Meat Supreme", "Pepperoni, Ham, Bacon", 0),
    "vegetarian": ("Vegetarian", "Mushrooms, Peppers, Olives", 0),
    "supreme":    ("Supreme", "Pepperoni, Ham, Mushrooms, Peppers, Olives, Onion", PRICE_SUPREME),
    "chicken":    ("Chicken", "Chicken, Red Onion, Spinach", PRICE_CHICKEN),
}


# ---------- helper functions ----------

def ask_yes_no(question):
    while True:
        answer = input(question + " (yes/no)\n").strip().lower()
        if answer in ("yes", "y"):
            return True
        if answer in ("no", "n"):
            return False
        print("Please type yes or no.")


def ask_choice(question, options):
    while True:
        answer = input(question + "\n").strip().lower()
        if answer in options:
            return answer
        print("Please type one of: " + " / ".join(options))


# ---------- 1. Welcome ----------

print("====================================")
print("    MARIO'S PIZZERIA KIOSK  v1.1    ")
print("====================================")

# ---------- 2. Name and size ----------

name = input("What is your name?\n").strip().title()
size = ask_choice("Would you like a Small ($10) or Large ($15) pizza?", ["small", "large"])
if size == "small":
    total_cost = PRICE_SMALL
else:
    total_cost = PRICE_LARGE

# ---------- 3. Dietary ----------

print(" --- DIETARY REQUIREMENTS --- ")
gluten_free = ask_yes_no("Do you need a Gluten-Free base?")
if gluten_free:
    total_cost = total_cost + PRICE_GLUTEN_FREE
    print("Added $3 for Gluten-Free base.")

dairy_free = ask_yes_no("Do you need Dairy-Free cheese?")
if dairy_free:
    total_cost = total_cost + PRICE_DAIRY_FREE
    print("Added $2 for Dairy-Free cheese.")

# ---------- 4. Sauce, toppings, crust (new in v1.1) ----------

print(" --- BUILD YOUR PIZZA --- ")
sauce = ask_choice("Tomato or BBQ sauce? (BBQ +$1)", ["tomato", "bbq"])
if sauce == "bbq":
    total_cost = total_cost + PRICE_BBQ
    print("Added $1 for BBQ sauce.")

print("Topping sets: classic ($0), vegetarian ($0), supreme (+$3), chicken (+$3)")
topping_key = ask_choice("Which topping set?", list(TOPPINGS))
topping_name, topping_items, topping_price = TOPPINGS[topping_key]
total_cost = total_cost + topping_price
print(topping_name + " toppings selected (" + topping_items + ").")
if topping_price:
    print("Added $" + str(topping_price) + " for " + topping_name + " toppings.")

crispy = ask_yes_no("Extra Crispy crust? (+$1)")
if crispy:
    total_cost = total_cost + PRICE_CRISPY
    print("Added $1 for Extra Crispy crust.")

# ---------- 5. Extras ----------

print(" --- EXTRAS --- ")
garlic_bread = ask_yes_no("Would you like a side of Garlic Bread ($4)?")
if garlic_bread:
    total_cost = total_cost + PRICE_GARLIC_BREAD
    print("Added $4 for Garlic Bread.")

delivery = ask_choice("Is this for delivery or pickup?", ["delivery", "pickup"]) == "delivery"
if delivery:
    total_cost = total_cost + PRICE_DELIVERY
    print("Added $5 delivery fee.")

# ---------- 6. Receipt ----------

dietary_notes = []
if gluten_free:
    dietary_notes.append("Gluten-Free base")
if dairy_free:
    dietary_notes.append("Dairy-Free cheese")

receipt = [
    "====================================",
    "       MARIO'S PIZZERIA KIOSK",
    "====================================",
    "Thank you, " + name + "!",
    "Pizza size: " + size.title() + (" (Extra Crispy)" if crispy else ""),
    "Sauce: " + ("BBQ" if sauce == "bbq" else "Tomato"),
    "Dietary notes: " + (", ".join(dietary_notes) if dietary_notes else "none"),
    "Toppings: " + topping_name + " (" + topping_items + ")",
]
if garlic_bread:
    receipt.append("Side: Garlic Bread")
receipt.append("Delivery or pickup: " + ("Delivery" if delivery else "Pickup"))
receipt.append("Your total for your custom pizza comes to: $" + str(total_cost))
receipt.append("====================================")

print(" ------------------------------------ ")
for line in receipt:
    print(line)

# ---------- 7. Save: receipt.txt, last_order.json, orders.log ----------

with open("receipt.txt", "w") as f:
    f.write("\n".join(receipt) + "\n")

order = {
    "name": name, "size": size, "gluten_free": gluten_free, "dairy_free": dairy_free,
    "vegetarian": topping_key == "vegetarian",      # keeps pizza_turtle.py happy
    "toppings": topping_key, "sauce": sauce, "crispy": crispy,
    "garlic_bread": garlic_bread, "delivery": delivery, "total_cost": total_cost,
}
with open("last_order.json", "w") as f:
    json.dump(order, f, indent=2)

# One line per order. Same layout as the Download orders.log button on enhanced.html.
when = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_line = (
    when + " | " + name + " | " + size + ("+crispy" if crispy else "")
    + " | sauce=" + sauce + " | toppings=" + topping_key
    + " | GF=" + ("Y" if gluten_free else "N") + " DF=" + ("Y" if dairy_free else "N")
    + " | garlic=" + ("Y" if garlic_bread else "N")
    + " | " + ("delivery" if delivery else "pickup")
    + " | $" + str(total_cost)
)
with open("orders.log", "a") as f:       # "a" = append, so old orders stay
    f.write(log_line + "\n")

print("Saved receipt.txt (email or text it), logged to orders.log.")
print("Run  python pizza_turtle.py  to watch Shelly draw it.")
