defmodule FirehoseWeb.LayoutsTest do
  use ExUnit.Case, async: true

  alias FirehoseWeb.Layouts

  @conn %Plug.Conn{
    assigns: %{}
  }

  describe "put_content_for/3" do
    test "stores content under the given key" do
      conn = put_content_for(@conn, :meta_tags, %{og_title: "Test"})

      assert conn.assigns[:content_for][:meta_tags] == [%{og_title: "Test"}]
    end

    test "appends multiple calls to the same key" do
      conn =
        @conn
        |> put_content_for(:meta_tags, %{og_title: "First"})
        |> put_content_for(:meta_tags, %{og_title: "Second"})

      assert length(conn.assigns[:content_for][:meta_tags]) == 2
      assert Enum.at(conn.assigns[:content_for][:meta_tags], 0).og_title == "First"
      assert Enum.at(conn.assigns[:content_for][:meta_tags], 1).og_title == "Second"
    end

    test "stores different keys independently" do
      conn =
        @conn
        |> put_content_for(:meta_tags, %{og_title: "Meta"})
        |> put_content_for(:scripts, "%(script)")

      assert Map.has_key?(conn.assigns[:content_for], :meta_tags)
      assert Map.has_key?(conn.assigns[:content_for], :scripts)
    end
  end

  describe "get_content_for/2" do
    test "returns stored content for a key" do
      content_for = %{meta_tags: [%{og_title: "Test"}]}

      assert Layouts.get_content_for(%{content_for: content_for}, :meta_tags) ==
               [%{og_title: "Test"}]
    end

    test "returns empty list when key does not exist" do
      assert Layouts.get_content_for(%{content_for: %{}}, :nonexistent) == []
    end

    test "returns empty list when content_for is nil" do
      assert Layouts.get_content_for(%{content_for: nil}, :nonexistent) == []
    end
  end

  # Re-export for use in tests
  defp put_content_for(conn, key, content), do: Layouts.put_content_for(conn, key, content)
end
