import plotly.graph_objects as go


import plotly.io as pio


from plotly.subplots import make_subplots


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


MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]


def load_data():
    return {"Harbor": [81, 83, 82, 85, 86, 88], "Hill": [94, 95, 96, 95, 97, 98]}


def make_after(data):
    fig = go.Figure()
    for (depot, scores), hue in zip(data.items(), WARM_EDITORIAL_PALETTE):
        fig.add_scatter(x=MONTHS, y=scores, mode="lines+markers", name=depot, line_color=hue)
    fig.add_hline(
        y=95,
        line_dash="dot",
        line_color=MUTED,
        annotation_text="95% target",
        annotation_position="top right",
    )
    fig.update_layout(
        template="warm_editorial",
        title_text="Harbor improved but missed the June target",
        yaxis={"title": "On time (%)", "range": [78, 100]},
        height=390,
        margin={"t": 60, "b": 85, "l": 60, "r": 15},
        legend={"orientation": "h", "y": 1.02, "x": 0.60, "yanchor": "bottom"},
    )
    fig.add_annotation(
        text="January–June · Cancelled runs excluded",
        x=0,
        y=-0.20,
        xanchor="left",
        xref="paper",
        yref="paper",
        showarrow=False,
        font={"size": 12, "color": MUTED},
    )
    return fig


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