import plotly.graph_objects as go


def load_data():
    return [
        ("Central", 82),
        ("West", 79),
        ("East", 77),
        ("South", 73),
        ("North", 69),
    ]


def value_table(data):
    return {
        "columns": ["Branch", "Renewals (%)"],
        "rows": [list(row) for row in data],
    }


def _renewal_chart(data, commentary: str) -> go.Figure:
    branches, rates = zip(*data, strict=True)
    fig = go.Figure(go.Bar(x=branches, y=rates, marker_color="#246487"))
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={
            "text": "Membership renewals by branch",
            "x": 0.08,
            "subtitle": {"text": commentary},
        },
        yaxis={"title": "Renewal rate", "range": [0, 100], "ticksuffix": "%"},
        margin={"l": 68, "r": 28, "t": 85, "b": 48},
    )
    return fig


def make_after(data) -> go.Figure:
    return _renewal_chart(
        data,
        "Central leads North by 13 percentage points. West leads East by 2 points.",
    )


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