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
      conn = get(conn, ~p"/blog/engineering")
      html = html_response(conn, 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
      conn = get(conn, ~p"/blog/engineering/future-test-post")
      assert html_response(conn, 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
      conn = get(conn, ~p"/blog/engineering/tag/test")
      html = html_response(conn, 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
      conn = get(conn, ~p"/blog/engineering/hello-world")

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

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

      response = html_response(conn, 200)
      assert response =~ "scheduled for"
      assert response =~ "January 01, 2099"
    end

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

      response = html_response(conn, 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
end
