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_before(data) -> go.Figure:
    stations, days, nights = zip(*data, strict=True)
    totals = [day + night for day, night in zip(days, nights, strict=True)]
    fig = go.Figure()
    for label, counts, color in (
        ("Day shift", days, "#25638a"),
        ("Night shift", nights, "#a3cee0"),
    ):
        fig.add_bar(
            y=stations,
            x=[100 * count / total for count, total in zip(counts, totals, strict=True)],
            orientation="h",
            name=label,
            marker_color=color,
            hovertemplate="%{y}: %{x:.0f}%<extra>%{fullData.name}</extra>",
        )
    fig.update_layout(
        template="simple_white",
        font=FONT,
        title="Emergency calls by station and shift",
        barmode="stack",
        xaxis={"title": "Share of calls", "ticksuffix": "%", "range": [0, 100]},
        yaxis={"autorange": "reversed"},
        legend={"orientation": "h", "y": 1.02},
        margin={"l": 90, "r": 25, "t": 84, "b": 62},
    )
    return fig


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