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,
        },
    )
)


import numpy as np


from plotly.subplots import make_subplots


def load_data():
    rng = np.random.default_rng(23)
    return {
        shift: sorted(np.round(rng.lognormal(center, 0.35, size), 1).tolist())
        for shift, center, size in [("Morning", 2.0, 80), ("Evening", 2.4, 120)]
    }


def make_after(data):
    fig = make_subplots(
        rows=2, cols=1, shared_xaxes=True, row_heights=[0.7, 0.3], vertical_spacing=0.16
    )
    for (shift, values), color, dash in zip(
        data.items(), (MUTED, WARM_EDITORIAL_PALETTE[0]), ("dot", "solid"), strict=True
    ):
        fig.add_scatter(
            x=[0] + values,
            y=[0] + [(i + 1) / len(values) for i in range(len(values))],
            mode="lines",
            name=shift,
            line=dict(shape="hv", color=color, dash=dash),
            row=1,
            col=1,
        )
        fig.add_annotation(x=values[-1], y=1, text=shift, showarrow=False, yshift=11, row=1, col=1)
        quantiles = np.quantile(values, (np.arange(10) + 0.5) / 10)
        fig.add_scatter(
            x=quantiles.tolist(),
            y=[shift] * 10,
            mode="markers",
            showlegend=False,
            marker=dict(color=color, size=8, symbol="diamond-open"),
            row=2,
            col=1,
        )
    fig.add_hline(y=0.9, line_color=INK, line_dash="dash", row=1, col=1)
    fig.update_xaxes(title_text="Call duration (minutes)", row=2, col=1)
    fig.update_yaxes(title_text="Share completed", tickformat=".0%", row=1, col=1)
    fig.update_layout(
        template="warm_editorial",
        title="Evening calls take longer at matched percentiles",
        title_subtitle_text="Each diamond represents one tenth of a shift's calls.",
        showlegend=False,
        yaxis_range=[-0.03, 1.14],
        margin=dict(t=95, b=55),
    )
    return fig


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