defmodule WhoNeedHelpWeb.SupportControllerTest do
  use WhoNeedHelpWeb.ConnCase, async: false

  import WhoNeedHelp.AccountsFixtures
  import Swoosh.TestAssertions

  describe "public support and removal intake" do
    test "renders each public form", %{conn: conn} do
      assert html_response(get(conn, ~p"/support"), 200) =~ "Contact Who Need Help"
      assert html_response(get(conn, ~p"/account/delete"), 200) =~ "Request account deletion"

      assert html_response(get(conn, ~p"/legal/content-removal"), 200) =~
               "Report illegal or harmful content"

      assert html_response(
               get(conn, ~p"/legal/content-removal?location=/requests/specific-id"),
               200
             ) =~ "/requests/specific-id"

      assert html_response(get(conn, ~p"/legal/take-it-down"), 200) =~
               "Do not upload the image or video"
    end

    test "creates support request without exposing its status token in the redirect", %{
      conn: conn
    } do
      conn =
        post(conn, ~p"/support", %{
          "support_request" => %{
            "kind" => "technical_issue",
            "contact_email" => "person@example.com",
            "subject" => "Unable to open a request",
            "details" => "The request page reports an error after the form is submitted."
          }
        })

      location = redirected_to(conn)
      assert location =~ "/support/received?reference=SUP-"
      refute location =~ "token="
      assert_email_sent()
    end

    test "creates urgent TAKE IT DOWN notice without accepting a media upload", %{conn: conn} do
      conn =
        post(conn, ~p"/legal/take-it-down", %{
          "notice" => %{
            "category" => "non_consensual_intimate_media",
            "submitter_name" => "Affected Person",
            "contact_email" => "affected@example.com",
            "relationship" => "self",
            "content_locations" => "https://example.test/requests/reported",
            "explanation" => "The identified intimate image was posted without my consent.",
            "electronic_signature" => "Affected Person",
            "good_faith" => "true",
            "accurate_complete" => "true"
          }
        })

      location = redirected_to(conn)
      assert location =~ "/legal/content-removal/received?reference=REM-"
      refute location =~ "token="
      assert_email_sent()
    end

    test "rejects malformed public form shapes without crashing", %{conn: conn} do
      assert response(post(conn, ~p"/support", %{"support_request" => "invalid"}), 400)

      assert response(
               post(conn, ~p"/legal/content-removal", %{"notice" => "invalid"}),
               400
             )

      assert response(
               post(conn, ~p"/legal/take-it-down", %{"notice" => "invalid"}),
               400
             )
    end
  end

  describe "authenticated support" do
    setup :register_and_log_in_user

    test "lists the current user's cases and links account deletion from settings", %{
      conn: conn,
      user: user
    } do
      {:ok, request} =
        WhoNeedHelp.Support.create_request(user_scope_fixture(user), %{
          "kind" => "account_access",
          "subject" => "Account question",
          "details" => "I need assistance understanding an account setting."
        })

      assert html_response(get(conn, ~p"/support/requests"), 200) =~ request.reference
      assert html_response(get(conn, ~p"/users/settings"), 200) =~ ~p"/account/delete"
    end
  end

  describe "operator queue authorization" do
    test "requires a moderator", %{conn: conn} do
      conn = get(conn, ~p"/support/operations")
      assert redirected_to(conn) == ~p"/users/log-in"
    end

    test "renders separate support and removal queues for a moderator", %{conn: conn} do
      moderator =
        user_fixture()
        |> Ecto.Changeset.change(role: :moderator)
        |> WhoNeedHelp.Repo.update!()

      {:ok, _support_request} =
        WhoNeedHelp.Support.create_request(nil, %{
          "kind" => "technical_issue",
          "contact_email" => "support-queue@example.com",
          "subject" => "Check the support queue",
          "details" => "This request verifies that an open status remains selected."
        })

      {:ok, _removal_notice} =
        WhoNeedHelp.ContentRemoval.create_notice(nil, :general, %{
          "category" => "illegal_content",
          "submitter_name" => "Queue Reporter",
          "contact_email" => "removal-queue@example.com",
          "relationship" => "self",
          "content_locations" => "https://example.test/requests/queue-check",
          "explanation" => "This notice verifies that an open status remains selected.",
          "electronic_signature" => "Queue Reporter",
          "good_faith" => "true",
          "accurate_complete" => "true"
        })

      response =
        conn
        |> log_in_user(moderator)
        |> get(~p"/support/operations")
        |> html_response(200)

      assert response =~ "Content removal and TAKE IT DOWN"
      assert response =~ "General support and data requests"

      document = LazyHTML.from_document(response)

      assert Enum.count(
               LazyHTML.query(
                 document,
                 "select[id^='removal-'] option[value='open'][selected]"
               )
             ) == 1

      assert Enum.count(
               LazyHTML.query(
                 document,
                 "select[id^='support-'] option[value='open'][selected]"
               )
             ) == 1
    end
  end
end
