defmodule WhoNeedHelp.PaginationTest do
  use ExUnit.Case, async: true

  alias WhoNeedHelp.Pagination

  test "cursor round-trips a UTC timestamp and UUID" do
    timestamp = ~U[2026-07-19 01:30:00Z]
    id = Ecto.UUID.generate()

    assert {:ok, {^timestamp, ^id}} = timestamp |> Pagination.encode(id) |> Pagination.decode()
  end

  test "invalid cursors never become query values" do
    assert :error = Pagination.decode("not-a-cursor")
    assert Pagination.cursor(after: "not-a-cursor") == nil
  end

  test "page exposes a cursor only when another row exists" do
    rows = [
      %{id: Ecto.UUID.generate(), inserted_at: ~U[2026-07-19 01:30:03Z]},
      %{id: Ecto.UUID.generate(), inserted_at: ~U[2026-07-19 01:30:02Z]},
      %{id: Ecto.UUID.generate(), inserted_at: ~U[2026-07-19 01:30:01Z]}
    ]

    page = Pagination.page(rows, 2, &{&1.inserted_at, &1.id})
    assert page.entries == Enum.take(rows, 2)
    assert is_binary(page.next_cursor)

    last_page = Pagination.page(Enum.take(rows, 2), 2, &{&1.inserted_at, &1.id})
    assert last_page.next_cursor == nil
  end
end
