import datetime

food_dict = {}

# ─────────────────────────────────────────────
# Add food item
# ─────────────────────────────────────────────

def add_food_item():
    name = ""
    while not name:
        name = input("Food name: ")

    while True:
        try:
            quantity = float(input("Quantity: "))
            if quantity > 0:
                break
            print("Quantity must be greater than 0.")
        except ValueError:
            print("Invalid quantity. Enter a number.")

    measure = ""
    while not measure:
        measure = input("Measure: ")

    while True:
        try:
            calories = int(input("Calories: "))
            if calories > 0:
                break
            print("Calories must be greater than 0.")
        except ValueError:
            print("Invalid calories. Enter a whole number.")

    # timestamp as key — guarantees uniqueness, automates date entry
    # comma in format allows splitting date from time later
    timestamp = datetime.datetime.now().strftime("%Y-%m-%d,%H:%M:%S")
    food_dict[timestamp] = [name, quantity, measure, calories]

# ─────────────────────────────────────────────
# Filter last 7 days
# ─────────────────────────────────────────────

def last_days_details():
    today = datetime.datetime.now()
    last_days_dict = {}

    for n in range(0, 8):
        reference_day = (today - datetime.timedelta(days=n)).strftime("%Y-%m-%d")
        for key in food_dict:
            if reference_day == key.split(",")[0]:
                last_days_dict[key] = food_dict[key]

    return last_days_dict

# ─────────────────────────────────────────────
# Print details
# ─────────────────────────────────────────────

def print_details():
    food_log = last_days_details()
    for key in food_log:
        print(f"{key.split(',')[0]} - {food_log[key][0]} - {food_log[key][1]}{food_log[key][2]} - {food_log[key][3]} kcal")

# ─────────────────────────────────────────────
# Menu
# ─────────────────────────────────────────────

while True:
    option = input("\nChoose option (1-Add, 2-Show food details, q-Quit): ")
    if option == "1":
        add_food_item()
        print("Food log added successfully!")
    elif option == "2":
        if len(last_days_details()):
            print_details()
        else:
            print("No data available.")
    elif option == "q":
        break
    else:
        print("Invalid option!")
