import plotly.graph_objects as go


FONT = {"family": "Inter, system-ui, -apple-system, sans-serif", "color": "#1f2937"}


def load_data():
    return [
        ("North", 12),
        ("Ridge", 16),
        ("Canal", 21),
        ("Park", 27),
        ("Harbor", 33),
        ("Market", 40),
        ("South", 48),
    ]


def value_table(data):
    return {
        "columns": ["District", "Weekly rainfall (mm)"],
        "rows": [list(row) for row in data],
    }


def _rainfall_strip(data, colorscale):
    districts, rainfall = zip(*data, strict=True)
    fig = go.Figure(
        go.Heatmap(
            x=districts,
            y=[""],
            z=[rainfall],
            zmin=10,
            zmax=50,
            colorscale=colorscale,
            xgap=2,
            colorbar={
                "title": {"text": "Rainfall (mm)", "side": "top"},
                "orientation": "h",
                "x": 0.5,
                "y": -0.38,
                "len": 0.8,
                "thickness": 12,
                "tickvals": [10, 20, 30, 40, 50],
            },
            hovertemplate="%{x}: %{z} mm<extra></extra>",
        )
    )
    fig.update_layout(
        template="simple_white",
        font=FONT,
        title="Weekly rainfall across seven districts",
        xaxis={"tickangle": 0},
        yaxis={"visible": False},
        height=265,
        margin={"l": 22, "r": 22, "t": 62, "b": 100},
    )
    return fig


def make_after(data) -> go.Figure:
    return _rainfall_strip(
        data,
        [
            [0.0, "#eff6ff"],
            [0.25, "#bfdbfe"],
            [0.5, "#60a5fa"],
            [0.75, "#2563eb"],
            [1.0, "#1e3a8a"],
        ],
    )


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