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


def load_data():
    return [
        (20, 3.2),
        (21, 3.6),
        (22, 4.1),
        (23, 4.6),
        (24, 4.9),
        (25, 5.6),
        (26, 5.3),
        (27, 4.7),
        (28, 4.1),
    ]


def clock_label(hour):
    return f"{hour % 24:02d}:00"


def make_after(data):
    hours, readings = zip(*data, strict=True)
    fig = go.Figure(go.Scatter(x=hours, y=readings, mode="lines+markers"))
    fig.add_hrect(y0=2, y1=5, fillcolor="#D9E8DE", line_width=0, layer="below")
    fig.add_annotation(x=20.2, y=2.3, text="Target 2–5 °C", showarrow=False, xanchor="left")
    fig.add_annotation(x=25, y=5.6, text="Peak 5.6 °C", showarrow=True, ax=30, ay=-25)
    ticks = [20, 22, 24, 26, 28]
    fig.update_layout(
        template="warm_editorial",
        title="Cargo warmed above target after midnight",
        xaxis_title="Time on the overnight route",
        yaxis_title="Temperature (°C)",
        xaxis_tickvals=ticks,
        xaxis_ticktext=[clock_label(h) for h in ticks],
        yaxis_range=[1.8, 6.5],
        showlegend=False,
    )
    return fig


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