import plotly.graph_objects as go


def load_data():
    return [
        ("Cedar", 48, 1200),
        ("Elm", 35, 700),
        ("Maple", 30, 300),
        ("Oak", 24, 400),
        ("Pine", 18, 200),
    ]


def value_table(data):
    return {
        "columns": ["Clinic", "Missed visits", "Booked visits"],
        "rows": [list(row) for row in data],
    }


def make_after(data) -> go.Figure:
    rates = sorted(
        ((clinic, 100 * missed / booked) for clinic, missed, booked in data),
        key=lambda row: -row[1],
    )
    clinics, percentages = zip(*rates, strict=True)
    fig = go.Figure(
        go.Bar(
            x=percentages,
            y=clinics,
            orientation="h",
            marker_color="#356890",
            hovertemplate="%{y}: %{x:g}%<extra></extra>",
        )
    )
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title="Share of booked visits missed by clinic",
        xaxis={"title": "Booked visits missed (%)", "range": [0, 12], "dtick": 2},
        yaxis_autorange="reversed",
        margin={"l": 74, "r": 24, "t": 56, "b": 60},
    )
    return fig


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