import plotly.graph_objects as go


FONT = {"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"}


def load_data():
    return [
        ("Canal", 4, 5, 8, 3, 4, 4),
        ("Cedar", 6, 7, 4, 6, 5, 5),
        ("Depot", 13, 3, 5, 4, 14, 3),
        ("Elm", 5, 5, 4, 4, 6, 5),
        ("Hill", 7, 4, 9, 5, 7, 8),
        ("Lake", 4, 6, 6, 6, 3, 6),
        ("Market", 7, 5, 5, 5, 8, 4),
        ("Park", 5, 4, 7, 5, 5, 6),
        ("Spruce", 6, 8, 5, 7, 6, 5),
        ("Union", 5, 6, 6, 4, 5, 7),
    ]


def value_table(data):
    return {
        "columns": [
            "Site",
            "Slow check-in",
            "Incorrect order",
            "Late pickup",
            "Missing receipt",
            "Queue delay",
            "Refund delay",
        ],
        "rows": [list(row) for row in data],
    }


def _site_profiles(data, *, ordered):
    attributes = value_table(data)["columns"]
    indices = (1, 5, 2, 4, 3, 6) if ordered else (1, 2, 3, 4, 5, 6)
    dimensions = [
        {
            "label": attributes[index],
            "values": [row[index] for row in data],
            "range": [0, 16],
            "tickvals": [0, 4, 8, 12, 16],
        }
        for index in indices
    ]
    line = {"color": "#64748b"}
    if ordered:
        line = {
            "color": [int(site == "Depot") for site, *_ in data],
            "cmin": 0,
            "cmax": 1,
            "colorscale": [[0, "#cbd5e1"], [1, "#1d608a"]],
        }
    fig = go.Figure(
        go.Parcoords(
            dimensions=dimensions, line=line, labelfont={"size": 12}, tickfont={"size": 11}
        )
    )
    title = {"text": "Service issues by site"}
    if ordered:
        title["subtitle"] = {"text": "Depot in blue. Counts per 100 orders on every axis."}
    fig.update_layout(
        template="simple_white",
        font=FONT,
        title=title,
        margin={"l": 56, "r": 56, "t": 98, "b": 42},
    )
    return fig


def make_after(data) -> go.Figure:
    return _site_profiles(data, ordered=True)


if __name__ == "__main__":
    print(make_after(load_data()).to_json())
