defmodule TarakanWeb.PageLiveTest do
  use TarakanWeb.ConnCase

  import Phoenix.LiveViewTest

  test "the disclosure policy page renders its sections", %{conn: conn} do
    html = conn |> get(~p"/policies/disclosure") |> html_response(200)

    assert html =~ "Disclosure policy"
    assert html =~ "Public by default"
    assert html =~ "Visibility levels"
    assert html =~ "public_summary"
    assert html =~ "restricted"
    assert html =~ "Verification quorum"
    assert html =~ "Vendor notification"
    assert html =~ "security.txt"
  end

  test "the content policy page renders its sections", %{conn: conn} do
    html = conn |> get(~p"/policies/content") |> html_response(200)

    assert html =~ "Content policy"
    assert html =~ "Allowed content"
    assert html =~ "Prohibited content"
    assert html =~ "Secrets and credentials"
    assert html =~ "Takedowns and appeals"
  end

  test "the pricing page renders the free record, bounties, and managed disclosure",
       %{conn: conn} do
    html = conn |> get(~p"/pricing") |> html_response(200)

    assert html =~ ~s(id="pricing-free")
    assert html =~ ~s(id="pricing-contracts")
    assert html =~ ~s(id="pricing-managed")
    assert html =~ "Tarakan keeps 10%"
    assert html =~ "mailto:"

    # No paid tiers, and the user-facing word is "contract", never "bounty".
    refute html =~ ~s(id="pricing-team")
    refute html =~ "Vaccine packs"
    refute html =~ "bounty"
    refute html =~ "Bounties"
  end

  test "the pricing checkout POST redirects anonymous visitors to login", %{conn: conn} do
    conn = post(conn, ~p"/billing/checkout", %{"plan" => "team"})

    assert redirected_to(conn) =~ "/accounts/log-in"
  end

  test "the managed disclosure page renders its sections", %{conn: conn} do
    html = conn |> get(~p"/services/disclosure") |> html_response(200)

    assert html =~ "Managed disclosure"
    assert html =~ "What it is"
    assert html =~ "Handling, not suppression"
    assert html =~ "Intake"
    assert html =~ ~s(id="managed-disclosure-contact")
    assert html =~ "mailto:"
  end

  test "the pricing checkout POST redirects to Stripe for members", %{conn: conn} do
    conn =
      conn
      |> log_in_account(account_fixture())
      |> post(~p"/billing/checkout", %{"plan" => "enterprise"})

    assert redirected_to(conn) =~ "https://checkout.stripe.com/"
  end

  # These were plain controller pages, the only routes in the shell without a
  # LiveSocket. That made navigating to them a full reload and left them out of
  # anything driven by live navigation.
  test "the static pages are LiveViews", %{conn: conn} do
    for path <- ["/pricing", "/policies/disclosure", "/policies/content", "/services/disclosure"] do
      assert {:ok, _view, _html} = live(conn, path)
    end
  end

  test "pricing marks its own nav entry active", %{conn: conn} do
    {:ok, _view, html} = live(conn, ~p"/pricing")

    assert html =~ ~r/href="\/pricing"[^>]*aria-current="page"/
  end

  test "nav links live-navigate between a LiveView and a policy page", %{conn: conn} do
    {:ok, view, _html} = live(conn, ~p"/leaderboard")

    assert {:ok, _pricing, html} =
             view
             |> element(~s(nav[aria-label="Primary"] a[href="/pricing"]))
             |> render_click()
             |> follow_redirect(conn, ~p"/pricing")

    assert html =~ "Pricing"
  end
end
