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 [
        (2020, 34, 65, 18),
        (2021, 38, 70, 21),
        (2022, 29, 59, 16),
        (2023, 37, 68, 23),
        (2024, 41, 74, 26),
        (2025, 45, 80, 29),
    ]


def make_after(data):
    years, library, depot, pool = zip(*data, strict=True)
    fig = go.Figure()
    for name, values, color in (
        ("Depot", depot, "#C7D9D4"),
        ("Library", library, "#98B7B0"),
        ("Pool", pool, "#568B83"),
    ):
        fig.add_scatter(
            x=years,
            y=values,
            name=name,
            mode="lines",
            fill="tozeroy",
            fillcolor=color,
            line={"width": 1.5, "color": INK},
        )
        fig.add_annotation(
            x=years[-1], y=values[-1], text=name, xanchor="left", xshift=4, showarrow=False
        )
    fig.update_layout(
        template="warm_editorial",
        title="Solar output recovered after the 2022 dip",
        xaxis_title="Year",
        xaxis_tickvals=years,
        yaxis_title="Generation (MWh)",
        yaxis_range=[0, 90],
        showlegend=False,
        margin={"r": 78},
    )
    return fig


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