defmodule FirehoseWeb.BlogControllerTest do
  use FirehoseWeb.ConnCase, async: false

  describe "GET /blog/:blog_id (index) - date filtering" do
    test "does not show future-dated posts", %{conn: conn} do
      html = conn |> get(~p"/blog/engineering") |> html_response(200)
      refute html =~ "Future Test Post"
    end
  end

  describe "GET /blog/:blog_id/:slug (show) - date filtering" do
    test "still shows a future-dated post by slug", %{conn: conn} do
      assert conn |> get(~p"/blog/engineering/future-test-post") |> html_response(200) =~
               "Future Test Post"
    end
  end

  describe "GET /blog/:blog_id/tag/:tag - date filtering" do
    test "excludes future-dated posts from tag page", %{conn: conn} do
      html = conn |> get(~p"/blog/engineering/tag/test") |> html_response(200)
      refute html =~ "Future Test Post"
    end
  end

  describe "GET /blog/:blog_id/:slug - status banners" do
    setup :register_and_log_in_user

    test "authenticated user sees draft banner on draft post", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/hello-world")

      assert html_response(response, 200) =~ "Draft"
      assert response.resp_body =~ "not published"
    end

    test "authenticated user sees scheduled banner on future post", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/future-test-post") |> html_response(200)

      assert response =~ "scheduled for"
      assert response =~ "January 01, 2099"
    end

    test "authenticated user sees no banner on live post", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/why-firehose") |> html_response(200)

      refute response =~ "Draft"
      refute response =~ "scheduled for"
    end
  end

  describe "GET /blog/:blog_id/:slug - no banners for unauthenticated" do
    test "unauthenticated user sees no banner on draft post", %{conn: conn} do
      response =
        conn
        |> get(~p"/blog/engineering/hello-world")
        |> html_response(200)

      refute response =~ "post-status-banner"
    end

    test "unauthenticated user sees no banner on future post", %{conn: conn} do
      response =
        conn
        |> get(~p"/blog/engineering/future-test-post")
        |> html_response(200)

      refute response =~ "post-status-banner"
    end
  end

  describe "GET /blog/:blog_id/:slug - Open Graph meta tags" do
    test "meta tags include og_title", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)

      assert response =~ ~s(<meta property="og:title")
      assert response =~ "Hello World"
    end

    test "meta tags include og_description", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)

      assert response =~ ~s(<meta property="og:description")
    end

    test "meta tags include og_type article", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)

      assert response =~ ~s(<meta property="og:type" content="article")
    end

    test "meta tags include twitter:card", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)

      assert response =~ ~s(<meta name="twitter:card" content="summary_large_image")
    end

    test "meta tags include og_image when post has image", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/why-firehose") |> html_response(200)

      assert response =~ ~s(<meta property="og:image")
      assert response =~ "firehose-logo.png"
    end

    test "meta tags have correct og_url", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering/hello-world") |> html_response(200)

      assert response =~
               ~s(<meta property="og:url" content="http://localhost:4002/blog/engineering/hello-world")
    end
  end

  describe "GET /blog/:blog_id - Open Graph meta tags" do
    test "meta tags include og_title for blog listing", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering") |> html_response(200)

      assert response =~ ~s(<meta property="og:title")
    end

    test "meta tags have og_type website for blog listing", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering") |> html_response(200)

      assert response =~ ~s(<meta property="og:type" content="website")
    end

    test "meta tags use summary twitter card for blog listing", %{conn: conn} do
      response = conn |> get(~p"/blog/engineering") |> html_response(200)

      assert response =~ ~s(<meta name="twitter:card" content="summary")
    end
  end
end
