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


def load_data():
    rng = np.random.default_rng(16)
    return [
        (route, old, list(rng.normal(old - saving, 2, 30)))
        for route, old, saving in zip(
            ("Bay", "Elm", "Hill", "Lake", "Mill", "Park"),
            (15, 18, 21, 24, 27, 30),
            (1, 3, 2, 5, 4, 6),
            strict=True,
        )
    ]


def make_before(data):
    fig = go.Figure(
        go.Scatter(
            x=[x for _, x, _ in data],
            y=[float(np.mean(y)) for _, _, y in data],
            mode="markers",
            marker_color=MUTED,
        )
    )
    fig.add_annotation(text="Lake and Park", x=26, y=22, showarrow=False)
    fig.update_layout(
        template="warm_editorial",
        title="Scheduled and trial route times",
        xaxis_title="Scheduled minutes",
        yaxis_title="Mean trial minutes",
    )
    return fig


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