defmodule FirehoseWeb.BlogTagsTest do
  use FirehoseWeb.ConnCase

  defp goto_engineering_tag_page(conn, tag) do
    path = "/blog/engineering/tag/#{tag}"
    conn_res = get(conn, path)
    body = html_response(conn_res, 200)
    assert body =~ ~s(tagged "#{tag}")
    assert body =~ "Engineering Blog"
    body
  end

  defp goto_releases_tag_page(conn, tag) do
    path = "/blog/releases/tag/#{tag}"
    conn_res = get(conn, path)
    body = html_response(conn_res, 200)
    assert body =~ ~s(tagged "#{tag}")
    assert body =~ "Release Notes"
    body
  end

  describe "engineering blog tags" do
    test "GET /blog/engineering/tag/:tag shows tag page with all posts", %{conn: conn} do
      body = goto_engineering_tag_page(conn, "elixir")
      assert body =~ "Hello World"
    end

    test "GET /blog/engineering/tag/:tag page shows filtered posts", %{conn: conn} do
      body = goto_engineering_tag_page(conn, "phoenix")
      assert body =~ "Hello World"
    end

    test "GET /blog/engineering/tag/:tag page shows empty list for nonexistent tag", %{
      conn: conn
    } do
      conn_res = get(conn, "/blog/engineering/tag/nonexistent-tag")
      assert html_response(conn_res, 200) =~ ~s(tagged "nonexistent-tag")
    end
  end

  describe "release notes blog tags" do
    test "GET /blog/releases/tag/:tag shows tag page with all posts", %{conn: conn} do
      body = goto_releases_tag_page(conn, "release")
      assert body =~ "v0.1.0 Released"
    end

    test "GET /blog/releases/tag/:tag page shows filtered posts", %{conn: conn} do
      conn_res = get(conn, "/blog/releases/tag/nonexistent-tag")
      assert html_response(conn_res, 200) =~ ~s(tagged "nonexistent-tag")
    end
  end

  describe "tag URL pattern" do
    test "tag URLs follow pattern /blog/:blog_id/tag/:tag for engineering blog", %{conn: conn} do
      # Test that the tag route exists and works correctly
      conn_res1 = get(conn, "/blog/engineering/tag/elixir")
      assert html_response(conn_res1, 200) =~ ~s(tagged "elixir")

      conn_res2 = get(conn, "/blog/engineering/tag/phoenix")
      assert html_response(conn_res2, 200) =~ ~s(tagged "phoenix")
    end

    test "tag URLs follow pattern /blog/:blog_id/tag/:tag for releases blog", %{conn: conn} do
      # Test that the tag route exists and works correctly
      conn_res = get(conn, "/blog/releases/tag/release")
      assert html_response(conn_res, 200) =~ ~s(tagged "release")
    end

    test "nonexistent tags return 200 with empty post list", %{conn: conn} do
      conn_res = get(conn, "/blog/engineering/tag/nonexistent-tag")
      assert html_response(conn_res, 200)
    end
  end

  describe "tag page structure" do
    test "tag page has proper layout and back link", %{conn: conn} do
      body = goto_engineering_tag_page(conn, "elixir")

      assert body =~ "Engineering Blog"
      assert body =~ ~s(tagged "elixir")
      assert body =~ "All posts"
    end

    test "release tag page has proper layout and back link", %{conn: conn} do
      body = goto_releases_tag_page(conn, "release")

      assert body =~ "Release Notes"
      assert body =~ ~s(tagged "release")
      assert body =~ "All posts"
    end
  end
end
