import plotly.graph_objects as go


Row = tuple[str, int, int]


def value_table(data: list[Row]) -> dict[str, list]:
    return {"columns": ["Branch", "April loans", "May loans"], "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": "Library loans by branch", "x": 0.02, "xanchor": "left"},
        margin={"l": 72, "r": 32, "t": 94, "b": 60},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    return [
        ("Central", 164, 172),
        ("North", 112, 109),
        ("East", 138, 143),
        ("West", 95, 132),
        ("South", 150, 121),
    ]


def make_after(data: list[Row]) -> go.Figure:
    branches, april, may = zip(*data, strict=True)
    fig = go.Figure(
        [
            go.Bar(x=branches, y=april, name="April", marker_color="#1d5d84"),
            go.Bar(x=branches, y=may, name="May", marker_color="#a34b18"),
        ]
    )
    _style_figure(
        fig, yaxis={"title": "Loans", "rangemode": "tozero"}, xaxis_tickangle=0, barmode="group"
    )
    return fig.update_layout(showlegend=True, legend={"orientation": "h", "y": 1.06})


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