defmodule ToweropsWeb.DebugController do
  @moduledoc """
  Debug endpoints for troubleshooting. Requires superuser authentication.
  """

  use ToweropsWeb, :controller

  @doc """
  Display all request headers for debugging cloudflared visitor location headers.
  Available at /admin/headers (requires superuser authentication).
  """
  def headers(conn, _params) do
    # Extract all request headers
    headers = Map.new(conn.req_headers)

    conn
    |> put_resp_content_type("application/json")
    |> send_resp(
      200,
      Jason.encode!(
        %{
          headers: headers,
          remote_ip: conn.remote_ip |> :inet.ntoa() |> to_string(),
          request_path: conn.request_path,
          method: conn.method
        },
        pretty: true
      )
    )
  end
end
