defmodule WhoNeedHelpWeb.Router do
  use WhoNeedHelpWeb, :router

  import WhoNeedHelpWeb.UserAuth

  @secure_browser_headers %{
    "content-security-policy" =>
      "default-src 'self'; base-uri 'self'; frame-ancestors 'none'; object-src 'none'",
    "permissions-policy" => "geolocation=(self), camera=(), microphone=(), payment=(), usb=()"
  }

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {WhoNeedHelpWeb.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers, @secure_browser_headers
    plug :put_content_security_policy
    plug :fetch_current_scope_for_user
    plug :put_authenticated_cache_policy
    plug WhoNeedHelpWeb.Locale
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  pipeline :mobile do
    plug :accepts, ["json"]
    plug :fetch_session
    plug :protect_from_forgery
    plug :put_secure_browser_headers, @secure_browser_headers
    plug :put_content_security_policy
    plug :fetch_current_scope_for_user
    plug :put_authenticated_cache_policy
  end

  defp put_content_security_policy(conn, opts),
    do: WhoNeedHelpWeb.SecurityHeaders.put_content_security_policy(conn, opts)

  scope "/healthz", WhoNeedHelpWeb do
    pipe_through :api

    get "/live", HealthController, :live
    get "/ready", HealthController, :ready
  end

  scope "/", WhoNeedHelpWeb do
    get "/metrics", MetricsController, :show
  end

  if Application.compile_env(:who_need_help, :e2e_routes) do
    scope "/__e2e__", WhoNeedHelpWeb do
      pipe_through :api

      get "/map-tile.png", E2eTileController, :show
      post "/crash-node", E2eFailureController, :crash_node
    end
  end

  scope "/mobile", WhoNeedHelpWeb do
    pipe_through :mobile

    post "/tracking/:assignment_id/position", MobileTrackingController, :update
    post "/tracking/:assignment_id/stop", MobileTrackingController, :stop
  end

  scope "/", WhoNeedHelpWeb do
    pipe_through :browser

    get "/", PageController, :home
    get "/feedback", FeedbackController, :show
    get "/privacy", PageController, :privacy
    get "/safety", PageController, :safety
    get "/terms", PageController, :terms
    get "/support", SupportController, :new
    get "/support/new", SupportController, :new
    post "/support", SupportController, :create
    get "/support/received", SupportController, :received
    get "/support/cases/:id", SupportController, :show
    get "/account/delete", SupportController, :delete_account
    post "/account/delete", SupportController, :request_account_deletion
    get "/legal/content-removal", ContentRemovalController, :new
    post "/legal/content-removal", ContentRemovalController, :create
    get "/legal/content-removal/received", ContentRemovalController, :received
    get "/legal/content-removal/:id", ContentRemovalController, :show
    get "/legal/take-it-down", ContentRemovalController, :take_it_down
    post "/legal/take-it-down", ContentRemovalController, :create_take_it_down
  end

  # Other scopes may use custom stacks.
  # scope "/api", WhoNeedHelpWeb do
  #   pipe_through :api
  # end

  # Enable LiveDashboard and Swoosh mailbox preview in development
  if Application.compile_env(:who_need_help, :dev_routes) do
    # If you want to use the LiveDashboard in production, you should put
    # it behind authentication and allow only admins to access it.
    # If your application does not have an admins-only section yet,
    # you can use Plug.BasicAuth to set up some basic authentication
    # as long as you are also using SSL (which you should anyway).
    import Phoenix.LiveDashboard.Router

    scope "/dev" do
      pipe_through :browser

      live_dashboard "/dashboard", metrics: WhoNeedHelpWeb.Telemetry
      forward "/mailbox", Plug.Swoosh.MailboxPreview
    end
  end

  ## Authentication routes

  scope "/", WhoNeedHelpWeb do
    pipe_through [:browser, :redirect_if_user_is_authenticated]

    get "/users/register", UserRegistrationController, :new
    post "/users/register", UserRegistrationController, :create
    post "/auth/google/login", GoogleAuthController, :start_login
    post "/auth/google/register", GoogleAuthController, :start_registration
  end

  scope "/", WhoNeedHelpWeb do
    pipe_through [:browser, :require_authenticated_user]

    get "/users/settings", UserSettingsController, :edit
    put "/users/settings", UserSettingsController, :update
    get "/users/settings/confirm-email", UserSettingsController, :confirm_email_page
    post "/users/settings/confirm-email", UserSettingsController, :confirm_email
    post "/auth/google/link", GoogleAuthController, :start_link
    delete "/auth/google/link", GoogleAuthController, :disconnect
    get "/auth/social/:provider", SocialOAuthController, :request
    get "/auth/social/:provider/callback", SocialOAuthController, :callback
    get "/support/requests", SupportController, :index
  end

  scope "/", WhoNeedHelpWeb do
    pipe_through :browser

    live_session :authenticated,
      on_mount: [{WhoNeedHelpWeb.UserAuth, :ensure_authenticated}] do
      live "/requests", RequestLive.Index, :index
      live "/requests/new", RequestLive.New, :new
      live "/requests/:id", RequestLive.Show, :show
      live "/activities", ActivityLive.Index, :index
      live "/activities/new", ActivityLive.New, :new
      live "/activities/:id", ActivityLive.Show, :show
      live "/categories/proposals", CategoryProposalLive, :index
      live "/profile", ProfileLive, :edit
      live "/leaderboard", LeaderboardLive, :index
    end

    live_session :moderation,
      on_mount: [{WhoNeedHelpWeb.UserAuth, :ensure_moderator}] do
      live "/moderation", ModerationLive, :index
      live "/support/operations", SupportOperationsLive, :index
    end
  end

  scope "/", WhoNeedHelpWeb do
    pipe_through [:browser]

    get "/users/log-in", UserSessionController, :new
    post "/users/log-in", UserSessionController, :create
    get "/auth/google/callback", GoogleAuthController, :callback
    delete "/users/log-out", UserSessionController, :delete
  end
end
