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_after(data):
    week, standard, express = zip(*data, strict=True)
    fig = go.Figure(
        go.Scatter(
            x=week,
            y=[e / s for e, s in zip(express, standard, strict=True)],
            mode="lines+markers",
            line_color=WARM_EDITORIAL_PALETTE[0],
        )
    )
    fig.add_hline(y=1, line_color=MUTED, line_dash="dot")
    fig.update_layout(
        template="warm_editorial",
        title="Express took longer in weeks 3 and 8",
        xaxis=dict(title="Week", range=[0.6, 12.4], dtick=1),
        yaxis=dict(
            type="log",
            title="Express / standard delivery time",
            tickvals=[0.5, 1, 2],
            ticktext=["Half", "Equal", "Double"],
            range=[-0.42, 0.42],
        ),
    )
    return fig


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