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


WEEKS = ["W1", "W2", "W3", "W4"]


def load_data():
    return {
        "Harbor": [[12, 14, 15, 16], [15, 17, 20, 24]],
        "Hill": [[12, 13, 12, 14], [13, 13, 14, 15]],
    }


def make_after(data):
    fig = make_subplots(
        rows=2,
        cols=1,
        shared_xaxes=True,
        vertical_spacing=0.17,
        subplot_titles=("Harbor depot", "Hill depot"),
    )
    for row, (depot, months) in enumerate(data.items(), 1):
        for index, (month, values) in enumerate(zip(["May", "June"], months, strict=True)):
            fig.add_scatter(
                x=WEEKS,
                y=values,
                mode="lines+markers",
                name=month,
                legendgroup=month,
                showlegend=False,
                line={"color": WARM_EDITORIAL_PALETTE[index], "dash": "solid" if index else "dot"},
                row=row,
                col=1,
            )
            fig.add_annotation(
                x="W4",
                y=values[-1],
                text=month,
                xanchor="left",
                yshift=7 if index else -7,
                showarrow=False,
                row=row,
                col=1,
            )
    fig.update_xaxes(range=[-0.1, 3.5])
    fig.update_yaxes(range=[9, 26], title_text="Riders per bus")
    fig.update_layout(
        template="warm_editorial",
        height=480,
        title_text="Harbor load rose to 24 riders per bus",
        margin={"t": 90, "b": 75, "l": 70, "r": 20},
        legend={"orientation": "h", "y": 1.20, "x": 0.65},
    )
    fig.add_annotation(
        x=0,
        y=-0.16,
        xref="paper",
        yref="paper",
        xanchor="left",
        showarrow=False,
        text="Source: illustrative farebox boardings divided by logged departures each week.",
        font={"size": 12, "color": MUTED},
    )
    return fig


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