defmodule MvWeb.Components.CoreComponentsTableTest do
  @moduledoc """
  Tests for the CoreComponents table: row hover/focus and selected styling.
  """
  use MvWeb.ConnCase, async: true

  import Phoenix.LiveViewTest

  alias MvWeb.CoreComponents

  describe "table row_click styling" do
    test "when row_click is set, table rows have hover and focus-within ring classes" do
      rows = [%{id: "1", name: "Alice"}, %{id: "2", name: "Bob"}]

      assigns = %{
        id: "test-table",
        rows: rows,
        row_id: fn r -> "row-#{r.id}" end,
        row_click: fn _ -> nil end,
        row_item: &Function.identity/1,
        col: [
          %{
            __slot__: :col,
            label: "Name",
            inner_block: fn _socket, item -> [item[:name] || item["name"] || ""] end
          }
        ],
        dynamic_cols: [],
        action: []
      }

      html = render_component(&CoreComponents.table/1, assigns)

      assert html =~ "hover:ring-2"
      assert html =~ "focus-within:ring-2"
      assert html =~ "hover:ring-base-content/10"
    end

    test "when row_click is nil, table rows do not have hover ring classes" do
      rows = [%{id: "1", name: "Alice"}]

      assigns = %{
        id: "test-table",
        rows: rows,
        row_id: fn r -> "row-#{r.id}" end,
        row_click: nil,
        row_item: &Function.identity/1,
        col: [
          %{
            __slot__: :col,
            label: "Name",
            inner_block: fn _socket, item -> [item[:name] || ""] end
          }
        ],
        dynamic_cols: [],
        action: []
      }

      html = render_component(&CoreComponents.table/1, assigns)

      refute html =~ "hover:ring-2"
      refute html =~ "focus-within:ring-2"
    end
  end

  describe "table selected_row_id styling" do
    test "when selected_row_id matches a row id, that row has data-selected and ring-primary" do
      rows = [%{id: "one", name: "Alice"}, %{id: "two", name: "Bob"}]

      assigns = %{
        id: "test-table",
        rows: rows,
        row_id: fn r -> "row-#{r.id}" end,
        row_click: fn _ -> nil end,
        selected_row_id: "two",
        row_item: &Function.identity/1,
        col: [
          %{
            __slot__: :col,
            label: "Name",
            inner_block: fn _socket, item -> [item[:name] || ""] end
          }
        ],
        dynamic_cols: [],
        action: []
      }

      html = render_component(&CoreComponents.table/1, assigns)

      assert html =~ ~s(id="row-two")
      assert html =~ ~s(data-selected="true")
      assert html =~ "ring-primary"
    end

    test "when selected_row_id is nil, no row has data-selected" do
      rows = [%{id: "1", name: "Alice"}]

      assigns = %{
        id: "test-table",
        rows: rows,
        row_id: fn r -> "row-#{r.id}" end,
        row_click: nil,
        selected_row_id: nil,
        row_item: &Function.identity/1,
        col: [
          %{
            __slot__: :col,
            label: "Name",
            inner_block: fn _socket, item -> [item[:name] || ""] end
          }
        ],
        dynamic_cols: [],
        action: []
      }

      html = render_component(&CoreComponents.table/1, assigns)

      refute html =~ ~s(data-selected="true")
    end

    test "when row_selected? is set, multiple rows can have data-selected and ring-primary" do
      rows = [%{id: "a", name: "Alice"}, %{id: "b", name: "Bob"}, %{id: "c", name: "Claire"}]
      selected_ids = MapSet.new(["a", "c"])

      assigns = %{
        id: "test-table",
        rows: rows,
        row_id: fn r -> "row-#{r.id}" end,
        row_click: fn _ -> nil end,
        row_selected?: fn item -> MapSet.member?(selected_ids, item.id) end,
        row_item: &Function.identity/1,
        col: [
          %{
            __slot__: :col,
            label: "Name",
            inner_block: fn _socket, item -> [item[:name] || ""] end
          }
        ],
        dynamic_cols: [],
        action: []
      }

      html = render_component(&CoreComponents.table/1, assigns)

      # Two rows selected (a and c), one not (b)
      assert html =~ ~s(id="row-a")
      assert html =~ ~s(id="row-b")
      assert html =~ ~s(id="row-c")
      # data-selected appears twice (for row a and row c)
      assert String.contains?(html, ~s(data-selected="true"))
      assert html =~ "ring-primary"
    end
  end
end
