"""shipping_cost: works, but is hard to read and change. Test file for AI Compare (What the tech).

Refactor this file for readability. Its behaviour must not change at all.
"""


def shipping_cost(order):
    # order: dict with "country" (str), "weight_kg" (float), "subtotal_cents" (int),
    # "express" (bool) and optionally "member" (bool). Returns the cost in cents (int).
    if order["country"] == "US":
        if order["express"] == True:
            if order["weight_kg"] <= 1:
                cost = 1200
            else:
                if order["weight_kg"] <= 5:
                    cost = 1200 + int((order["weight_kg"] - 1) * 250)
                else:
                    cost = 1200 + int((5 - 1) * 250) + int((order["weight_kg"] - 5) * 400)
        else:
            if order["subtotal_cents"] >= 5000:
                cost = 0
            else:
                if order["weight_kg"] <= 1:
                    cost = 500
                else:
                    if order["weight_kg"] <= 5:
                        cost = 500 + int((order["weight_kg"] - 1) * 150)
                    else:
                        cost = 500 + int((5 - 1) * 150) + int((order["weight_kg"] - 5) * 300)
    else:
        if order["country"] == "CA" or order["country"] == "MX":
            if order["express"] == True:
                if order["weight_kg"] <= 1:
                    cost = 2500
                else:
                    if order["weight_kg"] <= 5:
                        cost = 2500 + int((order["weight_kg"] - 1) * 500)
                    else:
                        cost = 2500 + int((5 - 1) * 500) + int((order["weight_kg"] - 5) * 800)
            else:
                if order["weight_kg"] <= 1:
                    cost = 1500
                else:
                    if order["weight_kg"] <= 5:
                        cost = 1500 + int((order["weight_kg"] - 1) * 300)
                    else:
                        cost = 1500 + int((5 - 1) * 300) + int((order["weight_kg"] - 5) * 600)
        else:
            raise ValueError("we do not ship to " + order["country"])
    if "member" in order and order["member"] == True:
        if cost > 0:
            cost = cost - int(cost * 10 / 100)
    if order["weight_kg"] > 30:
        raise ValueError("too heavy")
    return cost
