import plotly.graph_objects as go


Row = tuple[str, str, int]


def value_table(data: list[Row]) -> dict[str, list]:
    return {"columns": ["Division", "Team", "Training hours"], "rows": [list(row) for row in data]}


def _style_figure(fig: go.Figure, **layout: object) -> go.Figure:
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Training hours by division and team", "x": 0.02, "xanchor": "left"},
        margin={"l": 72, "r": 32, "t": 84, "b": 64},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    return [
        ("Operations", "Dispatch", 42),
        ("Operations", "Fleet", 24),
        ("Operations", "Stores", 15),
        ("Services", "Intake", 29),
        ("Services", "Support", 34),
        ("Services", "Billing", 17),
    ]


def make_before(data: list[Row]) -> go.Figure:
    _, teams, hours = zip(*sorted(data, key=lambda row: row[2], reverse=True), strict=True)
    fig = go.Figure(go.Bar(x=hours, y=teams, orientation="h", marker_color="#226391"))
    return _style_figure(
        fig,
        xaxis={"title": "Training hours", "range": [0, 45]},
        yaxis={"autorange": "reversed", "automargin": True},
    )


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