import plotly.graph_objects as go


Row = tuple[str, int, int, str]


def value_table(data: list[Row]) -> dict[str, list]:
    return {
        "columns": ["Station", "Easting (m)", "Northing (m)", "Flood risk"],
        "rows": [list(row) for row in data],
    }


def _style_figure(fig: go.Figure, **layout: object) -> go.Figure:
    fig.update_layout(
        template="simple_white",
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"},
        title={"text": "Flood risk at inspection stations", "x": 0.02, "xanchor": "left"},
        margin={"l": 72, "r": 32, "t": 68, "b": 60},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    return [
        ("A", 100, 120, "Low"),
        ("B", 180, 155, "Medium"),
        ("C", 255, 165, "High"),
        ("D", 165, 240, "Medium"),
        ("E", 290, 260, "High"),
        ("F", 230, 300, "Low"),
    ]


def _risk_map(data: list[Row], marker_styles: dict[str, dict[str, object]]) -> go.Figure:
    fig = go.Figure()
    for risk, style in marker_styles.items():
        stations, east, north = zip(*(row[:3] for row in data if row[3] == risk), strict=True)
        fig.add_scatter(
            x=east,
            y=north,
            text=stations,
            name=risk,
            mode="markers+text",
            textposition="top center",
            marker={"size": 16, **style},
        )
    _style_figure(
        fig,
        xaxis={"title": "Easting (m)", "range": [65, 340]},
        yaxis={"title": "Northing (m)", "range": [75, 345], "scaleanchor": "x", "scaleratio": 1},
    )
    return fig.update_layout(showlegend=True, legend={"title": "Flood risk"})


def make_after(data: list[Row]) -> go.Figure:
    return _risk_map(
        data,
        {
            "Low": {"color": "#b2d0e1", "line": {"color": "#507591", "width": 1}},
            "Medium": {"color": "#578bb0"},
            "High": {"color": "#183d62"},
        },
    )


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