import plotly.graph_objects as go


Row = tuple[int, bool]


def value_table(data: list[Row]) -> dict[str, list]:
    return {
        "columns": ["Household", "Pickup missed"],
        "rows": [[household, "Yes" if missed else "No"] for household, missed 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": "Missed recycling pickups among 20 households",
            "x": 0.02,
            "xanchor": "left",
        },
        margin={"l": 72, "r": 32, "t": 84, "b": 64},
        showlegend=False,
        **layout,
    )
    return fig


def load_data() -> list[Row]:
    misses = {2, 5, 8, 11, 14, 17, 19}
    return [(household, household in misses) for household in range(1, 21)]


def _pickup_array(data: list[Row]) -> go.Figure:
    status = [missed for _, missed in data]
    fig = go.Figure(
        go.Scatter(
            x=[index % 5 for index in range(len(data))],
            y=[index // 5 for index in range(len(data))],
            mode="markers",
            marker={
                "symbol": "square",
                "size": 28,
                "color": ["#236493" if missed else "#dde5ed" for missed in status],
            },
            customdata=[
                [household, "Missed" if missed else "Collected"] for household, missed in data
            ],
            hovertemplate="Household %{customdata[0]}: %{customdata[1]}<extra></extra>",
        )
    )
    return _style_figure(
        fig,
        height=350,
        xaxis={"visible": False, "range": [-0.6, 4.6], "constrain": "domain"},
        yaxis={"visible": False, "range": [3.6, -0.6], "scaleanchor": "x", "scaleratio": 1},
    )


def make_before(data: list[Row]) -> go.Figure:
    fig = _pickup_array(data)
    fig.add_annotation(
        x=0.5,
        y=-0.13,
        xref="paper",
        yref="paper",
        text="Blue squares show missed pickups",
        showarrow=False,
    )
    return fig


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