# RACE Riyadh: Smart Charging for Self-Driving Electric Cars
# Player ID: ______________   (use your player ID, never your name)
#
# Save this file as  race_<yourPlayerID>.py
# Fill in each function where it says  # YOUR CODE HERE  (replace the word pass).
# Run the file after every step: the TEST AREA at the bottom shows what each answer should be.
# Do not change the function names.

RESERVE_KM = 3    # every car must arrive with at least 3 km of battery left


# =============================== PART 1 ===============================

# STEP 1: Does a car need charging?
# A car needs charging if its range is less than (distance + RESERVE_KM).
# Example: needs_charging(10, 12) -> True      needs_charging(20, 9) -> False
def needs_charging(range_km, distance_km):
    # YOUR CODE HERE
    pass


# STEP 2: How important is the trip?   (you will use this function in Step 7)
# impact = 1 + social + economic + 3 * health
# Example: impact(3, 3, 0) -> 7       impact(1, 0, 3) -> 11
def impact(social, economic, health):
    # YOUR CODE HERE
    pass


# STEP 3: Which car is the most urgent?
# Return the NAME of the car with the highest impact, among the cars that NEED charging.
# If two cars have the same impact, return the one that comes first in the list.
# If no car needs charging, return "none".
# Example (the paper's scenario) -> "Professor Moustafa"
def most_urgent(names, ranges, distances, impacts):
    # YOUR CODE HERE
    pass


# STEP 4: In what order should cars charge?
# Return a list of the names of ALL cars that need charging, most urgent first.
# Hint: use most_urgent again and again. Each time, add the car to your list and remove it
#       from COPIES of the lists (use .copy() so the original lists do not change).
# Example (the paper's scenario) -> ["Professor Moustafa", "Adam"]
def charging_order(names, ranges, distances, impacts):
    # YOUR CODE HERE
    pass


# STEP 5: Fix the bugs
# This function should count how many cars need charging, but it has TWO bugs.
# Do not edit it. Write a correct version in count_needing_charge below,
# and explain both bugs in your report.
def count_needing_charge_buggy(ranges, distances):
    count = 0
    for i in range(len(ranges) - 1):
        if distances[i] > ranges[i]:
            count = count + 1
    return count

# Examples: the paper's scenario -> 2      count_needing_charge([11, 20], [9, 9]) -> 1
# (Both examples must be right: the second one only works when BOTH bugs are fixed.)
def count_needing_charge(ranges, distances):
    # YOUR CODE HERE
    pass


# =============================== PART 2 ===============================
# The data files are JSON. Each file is a whole city, for example:
# {"lane_slots": 1,
#  "cars": [{"name": "Adam", "range_km": 10, "distance_km": 12, "social": 2, "economic": 1, "health": 0}, ...]}
# json.load turns it into a Python dictionary with a list of car dictionaries inside.
import json

# STEP 6: Read a city from a JSON file
# Return a dictionary {"lane_slots": ..., "cars": [...]} that contains only the GOOD cars.
# A car is bad if a key is missing, or a number is not a number (for example "ten"):
#   print it and skip it. Never crash.
# If the file does not exist, return {"lane_slots": 0, "cars": []}
# Example: load_city("data/city_bad.json") -> 3 good cars (Huda, Reem, Salem)
def load_city(filename):
    # YOUR CODE HERE
    pass


# STEP 7: The decision
# city is a dictionary like the one load_city returns (it may also contain other keys: ignore them).
# Return a list of orders, one per car. Each order is a dictionary: {"car": name, "action": ...}
#   - cars that need charging, most urgent first: the first lane_slots of them get "lane", the rest get "station"
#   - then every car that does NOT need charging gets "continue"
# Use your Part 1 functions.
# Example (city_A, 1 lane slot) -> [{"car": "Professor Moustafa", "action": "lane"},
#                                   {"car": "Adam", "action": "station"}, {"car": "Mohammed", "action": "continue"}]
def plan(city):
    # YOUR CODE HERE
    pass


# STEP 8: Save the orders to a JSON file (use json.dump)
def save_orders(orders, filename):
    # YOUR CODE HERE
    pass


# ============================== TEST AREA ==============================
if __name__ == "__main__":
    names = ["Mohammed", "Adam", "Professor Moustafa"]
    ranges = [14, 10, 6]
    distances = [9, 12, 15]
    impacts = [2, 4, 7]

    print("Step 1:", needs_charging(10, 12), needs_charging(20, 9), "  should be: True False")
    print("Step 2:", impact(3, 3, 0), impact(1, 0, 3), "  should be: 7 11")
    print("Step 3:", most_urgent(names, ranges, distances, impacts), "  should be: Professor Moustafa")
    print("Step 4:", charging_order(names, ranges, distances, impacts), "  should be: ['Professor Moustafa', 'Adam']")
    print("Step 5:", count_needing_charge(ranges, distances), count_needing_charge([11, 20], [9, 9]), "  should be: 2 1")

    # Part 2 (put the data folder next to this file)
    city = load_city("data/city_A.json")
    print("Step 6:", city)
    orders = plan(city)
    print("Step 7:", orders)
    save_orders(orders, "data/orders_A.json")
    print("Step 8: open data/orders_A.json and check it")
