defmodule MicrowavepropWeb.AnalyticsTest do
  @moduledoc """
  Ensures the Plausible analytics snippet is present on every page the
  app serves. Plausible lives in `root.html.heex` (loaded by every
  request through the root layout) precisely so /map, /weather, and the
  other full-bleed live views that bypass `<Layouts.app>` still load
  the tracker. These tests lock that behaviour in so a future refactor
  that moves the snippet back into `Layouts.app` can't silently drop
  analytics on the three highest-traffic pages.
  """
  use MicrowavepropWeb.ConnCase, async: true

  @plausible_script_url "https://a.w5isp.com/js/pa-3eKACFxtbw6MHrTDA28wG.js"

  # Pages in this list are fetched through the router and their rendered
  # HTML asserted to contain the Plausible snippet. Any HTML route that
  # a human-visible page lives behind belongs here.
  @pages_to_check [
    {"/map", "propagation map"},
    {"/contacts/map", "contact map"},
    {"/weather", "weather map"},
    {"/about", "about page"},
    {"/algo", "algorithm docs"},
    {"/privacy", "privacy policy"},
    {"/submit", "QSO submission form"},
    {"/contacts", "contact list"},
    {"/path", "path analysis"},
    {"/beacons", "beacon list"},
    {"/users/register", "registration form"},
    {"/users/log-in", "login form"}
  ]

  for {path, label} <- @pages_to_check do
    test "#{label} (#{path}) includes the Plausible script tag", %{conn: conn} do
      html = conn |> get(unquote(path)) |> html_response(200)

      assert html =~ unquote(@plausible_script_url),
             "Plausible script missing on #{unquote(path)} — check root.html.heex"
    end

    test "#{label} (#{path}) wires up window.plausible() via plausible.init", %{conn: conn} do
      html = conn |> get(unquote(path)) |> html_response(200)

      assert html =~ "window.plausible",
             "Plausible init stub missing on #{unquote(path)}"

      assert html =~ "plausible.init",
             "plausible.init() call missing on #{unquote(path)}"
    end
  end

  test "the script tag is emitted exactly once per page (no duplicates)", %{conn: conn} do
    # If the snippet accidentally returns to both `root.html.heex` AND
    # `Layouts.app`, pages that use `<Layouts.app>` would load the tag
    # twice and double-count pageviews. Assert a single occurrence on a
    # layout-wrapped page.
    html = conn |> get(~p"/about") |> html_response(200)
    occurrences = html |> String.split(@plausible_script_url) |> length()
    # String.split + length: N occurrences → N+1 pieces. One occurrence
    # is the expected state.
    assert occurrences == 2,
           "Plausible script appears #{occurrences - 1}×; expected 1 on /about"
  end

  test "the script loads asynchronously so it can't block the first paint", %{conn: conn} do
    html = conn |> get(~p"/about") |> html_response(200)

    assert html =~ ~r/<script\s+async\s+src="#{Regex.escape(@plausible_script_url)}"/,
           "Plausible script must be loaded with `async` to avoid blocking first paint"
  end

  test "registered-user pages still carry the snippet after login", %{conn: conn} do
    user =
      Microwaveprop.AccountsFixtures.user_fixture(%{
        password: "hunter2 hunter2 hunter2",
        password_confirmation: "hunter2 hunter2 hunter2"
      })

    conn = log_in_user(conn, user)
    html = conn |> get(~p"/users/settings") |> html_response(200)
    assert html =~ @plausible_script_url
  end
end
