import plotly.graph_objects as go


import plotly.io as pio


INK = "#1C1917"


MUTED = "#57534E"


WARM_EDITORIAL_PALETTE = [
    "#0F766E",
    "#C2410C",
    "#1D4ED8",
    "#A16207",
    "#7E22CE",
    "#475569",
    "#0369A1",
    "#1C1917",
]


CANVAS = "#FFF7ED"


RULE = "#E7DDD2"


pio.templates["warm_editorial"] = go.layout.Template(
    layout=go.Layout(
        colorway=WARM_EDITORIAL_PALETTE,
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "size": 13, "color": INK},
        title={
            "x": 0,
            "xanchor": "left",
            "font": {"family": "Georgia, 'Times New Roman', serif", "size": 18, "color": INK},
        },
        paper_bgcolor=CANVAS,
        plot_bgcolor=CANVAS,
        xaxis={
            "showgrid": False,
            "showline": True,
            "linecolor": "#A8A29E",
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "automargin": True,
        },
        yaxis={
            "showgrid": True,
            "gridcolor": RULE,
            "gridwidth": 1,
            "showline": False,
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "zeroline": False,
            "automargin": True,
        },
    )
)


def load_data():
    return [
        ["Phone", "Booked", 70],
        ["Phone", "Waitlist", 10],
        ["Online", "Booked", 50],
        ["Online", "Waitlist", 30],
    ]


def value_table(data):
    return {"columns": ["Request channel", "Outcome", "Requests"], "rows": data}


def make_after(data):
    labels = ["Phone", "Online", "Booked", "Waitlist"]
    fig = go.Figure(
        go.Sankey(
            arrangement="fixed",
            node=dict(
                label=labels,
                x=[0.02, 0.02, 0.98, 0.98],
                y=[0.24, 0.76, 0.38, 0.9],
                color=WARM_EDITORIAL_PALETTE[0],
                pad=22,
                thickness=16,
            ),
            link=dict(
                source=[labels.index(channel) for channel, _, _ in data],
                target=[labels.index(outcome) for _, outcome, _ in data],
                value=[n for _, _, n in data],
                color="rgba(15,118,110,0.35)",
            ),
        )
    )
    online = sum(n for channel, outcome, n in data if channel == "Online" and outcome == "Waitlist")
    total = sum(n for _, outcome, n in data if outcome == "Waitlist")
    fig.update_layout(
        template="warm_editorial",
        title_text=f"Online requests account for {online / total:.0%} of the waitlist",
        margin=dict(l=32, r=32, t=70, b=95),
    )
    fig.add_annotation(
        text=f"Band width counts requests. Online requests fill {online} of {total} waitlist places.<br>Source: Illustrative clinic booking log, May 2025.",
        xref="paper",
        yref="paper",
        x=0,
        y=-0.08,
        xanchor="left",
        yanchor="top",
        align="left",
        showarrow=False,
    )
    return fig


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