import plotly.graph_objects as go


Row = tuple[str, int]


def value_table(data: list[Row]) -> dict[str, list]:
    return {"columns": ["Month", "Missed deliveries"], "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": "Missed deliveries by month", "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 [("Jan", 12), ("Feb", 16), ("Mar", 14), ("Apr", 38), ("May", 17), ("Jun", 15)]


def _delivery_bars(data: list[Row], *, emphasize_peak: bool) -> go.Figure:
    months, counts = zip(*data, strict=True)
    peak = max(counts)
    colors = ["#1d5d84" if emphasize_peak and count == peak else "#a5b7c7" for count in counts]
    labels = [str(count) if emphasize_peak and count == peak else "" for count in counts]
    fig = go.Figure(
        go.Bar(x=months, y=counts, marker_color=colors, text=labels, textposition="outside")
    )
    return _style_figure(
        fig, xaxis_title="Month", yaxis={"title": "Missed deliveries", "range": [0, 45]}
    )


def make_after(data: list[Row]) -> go.Figure:
    return _delivery_bars(data, emphasize_peak=True)


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