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


from statistics import mean


def load_data():
    return [
        (week, standard, express)
        for week, (standard, express) in enumerate(
            zip(
                [16, 18, 19, 20, 19, 21, 22, 21, 22, 20, 19, 20],
                [8, 9, 30, 10, 9, 10, 11, 42, 11, 10, 9, 10],
                strict=True,
            ),
            1,
        )
    ]


def make_before(data):
    fig = go.Figure()
    for col, name, color in [(1, "Standard", MUTED), (2, "Express", WARM_EDITORIAL_PALETTE[0])]:
        fig.add_scatter(
            x=[1, 2, 3],
            y=[mean(row[col] for row in data[start : start + 4]) for start in (0, 4, 8)],
            mode="markers",
            name=name,
            line_color=color,
        )
    fig.update_layout(
        template="warm_editorial",
        title="Mean delivery time by four-week period",
        xaxis_title="Four-week period",
        yaxis_title="Hours",
        xaxis_range=[1, 3],
    )
    return fig


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