import plotly.graph_objects as go


Row = tuple[str, int]


def value_table(data: list[Row]) -> dict[str, list]:
    return {"columns": ["Request type", "Tickets"], "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": "Help desk tickets by request type", "x": 0.02, "xanchor": "left"},
        margin={"l": 72, "r": 32, "t": 68, "b": 60},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    return [
        ("Password reset", 86),
        ("Software install", 64),
        ("Device issue", 43),
        ("Suspected phishing", 11),
        ("Directory edit", 5),
        ("Badge reprint", 7),
        ("Name correction", 6),
    ]


def _ticket_bars(rows: list[Row]) -> go.Figure:
    labels, counts = zip(*rows, strict=True)
    fig = go.Figure(
        go.Bar(
            x=counts,
            y=labels,
            orientation="h",
            marker_color="#226391",
            text=counts,
            textposition="outside",
        )
    )
    return _style_figure(
        fig,
        xaxis={"title": "Tickets", "range": [0, 100]},
        yaxis={"autorange": "reversed", "automargin": True, "tickangle": 0},
    )


def make_before(data: list[Row]) -> go.Figure:
    return _ticket_bars([*data[:3], ("Other", sum(count for _, count in data[3:]))])


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