import plotly.graph_objects as go


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


def load_data():
    return [
        ("Canal", 90, 60),
        ("Central", 150, 50),
        ("Harbor", 60, 40),
        ("Ridge", 144, 36),
        ("South", 70, 70),
    ]


def value_table(data):
    return {
        "columns": ["Station", "Day shift", "Night shift"],
        "rows": [list(row) for row in data],
    }


def make_after(data) -> go.Figure:
    ranked = sorted(data, key=lambda row: row[1] + row[2], reverse=True)
    stations, days, nights = zip(*ranked, strict=True)
    totals = [day + night for day, night in zip(days, nights, strict=True)]
    fig = go.Figure()
    for axis, values in (("x", totals), ("x2", days), ("x3", nights)):
        fig.add_bar(
            x=values,
            y=stations,
            xaxis=axis,
            orientation="h",
            marker_color="#25638a",
            text=values,
            textposition="outside",
            cliponaxis=False,
            hoverinfo="skip",
        )
    scale = {"range": [0, 230], "tickvals": [0, 100, 200], "anchor": "y"}
    fig.update_layout(
        template="simple_white",
        font=FONT,
        showlegend=False,
        title="Emergency calls by station and shift",
        xaxis={**scale, "domain": [0, 0.28], "title": "All calls"},
        xaxis2={**scale, "domain": [0.36, 0.64], "title": "Day shift"},
        xaxis3={**scale, "domain": [0.72, 1], "title": "Night shift"},
        yaxis={"autorange": "reversed", "categoryorder": "array", "categoryarray": stations},
        margin={"l": 88, "r": 30, "t": 58, "b": 62},
    )
    return fig


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