import plotly.graph_objects as go


def load_data():
    return [
        ("Jan", 42, "Observed"),
        ("Feb", 44, "Observed"),
        ("Mar", 43, "Observed"),
        ("Apr", 48, "Observed"),
        ("May", 51, "Observed"),
        ("Jun", 54, "Forecast"),
        ("Jul", 58, "Forecast"),
        ("Aug", 60, "Forecast"),
    ]


def value_table(data):
    return {"columns": ["Month", "Demand (MWh)", "Status"], "rows": [list(row) for row in data]}


def demand_figure():
    fig = go.Figure()
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title="Monthly electricity demand",
        yaxis={"title": "Demand (MWh)", "range": [38, 66]},
        xaxis_tickangle=0,
        margin={"l": 76, "r": 36, "t": 56, "b": 52},
        showlegend=False,
    )
    return fig


def make_after(data) -> go.Figure:
    observed = [row for row in data if row[2] == "Observed"]
    forecast = [row for row in data if row[2] == "Forecast"]
    fig = demand_figure()
    for label, rows, dash, symbol in (
        ("Observed", observed, "solid", "circle"),
        ("Forecast", [observed[-1], *forecast], "dash", "circle-open"),
    ):
        months, demand, _ = zip(*rows, strict=True)
        fig.add_scatter(
            x=months,
            y=demand,
            mode="lines+markers",
            line={"color": "#356890", "dash": dash},
            marker={"symbol": symbol},
            customdata=[row[2] for row in rows],
            hovertemplate="%{x}: %{y} MWh (%{customdata})<extra></extra>",
        )
        fig.add_annotation(
            x=months[-1],
            y=demand[-1],
            text=label,
            showarrow=False,
            yshift=10,
            yanchor="bottom",
        )
    return fig


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