import plotly.graph_objects as go


FONT = {"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"}


def load_data():
    return [
        ("Cedar", 12, 4),
        ("Elm", 8, 6),
        ("Harbor", 10, 5),
        ("Park", 6, 8),
        ("River", 7, 7),
        ("Union", 5, 9),
    ]


def value_table(data):
    return {
        "columns": ["Clinic", "Staffed exam rooms", "Clinicians on duty"],
        "rows": [list(row) for row in data],
    }


def make_before(data) -> go.Figure:
    clinics, rooms, clinicians = zip(*data, strict=True)
    fig = go.Figure()
    for index, (_, room_count, clinician_count) in enumerate(data):
        fig.add_shape(
            type="rect",
            x0=index - room_count / 32,
            x1=index + room_count / 32,
            y0=0,
            y1=clinician_count,
            fillcolor="#4389aa",
            line={"width": 0},
        )
    fig.add_scatter(
        x=list(range(len(clinics))),
        y=[count / 2 for count in clinicians],
        mode="markers",
        marker={"size": 36, "opacity": 0},
        text=clinics,
        customdata=list(zip(rooms, clinicians, strict=True)),
        hovertemplate="%{text}: %{customdata[0]} rooms, %{customdata[1]} clinicians<extra></extra>",
    )
    fig.update_layout(
        template="simple_white",
        font=FONT,
        title={
            "text": "Clinic capacity and staffing",
            "subtitle": {"text": "Width shows exam rooms. Height shows clinicians."},
        },
        xaxis={"tickvals": list(range(len(clinics))), "ticktext": clinics, "range": [-0.55, 5.55]},
        yaxis={"title": "Clinicians on duty", "range": [0, 10], "dtick": 2},
        margin={"l": 80, "r": 28, "t": 86, "b": 55},
    )
    return fig


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