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(42)
    return {
        shift: list(np.round(rng.gamma(2.2, scale, 110), 1))
        for shift, scale in [("Morning", 1.3), ("Evening", 2.5)]
    }


def make_after(data):
    fig = make_subplots(
        rows=2,
        cols=1,
        shared_xaxes=True,
        vertical_spacing=0.15,
        subplot_titles=("Morning", "Evening"),
    )
    for row, (shift, values) in enumerate(data.items(), 1):
        counts, edges = np.histogram(values, bins=np.arange(0, 27, 2))
        fig.add_bar(
            x=(edges[:-1] + 1).tolist(),
            y=counts.tolist(),
            width=2,
            name=shift,
            row=row,
            col=1,
            marker=dict(color=WARM_EDITORIAL_PALETTE[0], line=dict(color=CANVAS, width=1)),
        )
    fig.update_yaxes(title_text="Waits", range=[0, 60])
    fig.update_xaxes(title_text="Wait (minutes)", row=2, col=1)
    fig.update_layout(
        template="warm_editorial", title="Evening waits extend farther", showlegend=False
    )
    return fig


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