# typed: strict
Rails.application.routes.draw do
  id_constraint = T.let(%r{([^/]+)}, Regexp)

  mount ActionCable.server => "/cable"

  # Authentication routes
  devise_for :users,
             controllers: {
               registrations: "users/registrations",
               sessions: "users/sessions",
             }

  root to: "pages#root"

  get "furecs-user-script", to: "pages#furecs_user_script"
  get "us/:script", to: "user_scripts#get", constraints: { script: /.*/ }

  namespace :api do
    namespace :fa do
      get :similar_users, to: "/domain/users#similar_users"
    end
  end

  resources :users,
            as: :domain_users,
            only: %i[show],
            constraints: {
              id: id_constraint,
            },
            controller: "domain/users" do
    get :search_by_name, on: :collection

    get "followed_by", on: :member, action: :followed_by
    get "following", on: :member, action: :following

    resources :job_events, only: [], controller: "domain/user_job_events" do
      get "tracked_objects",
          on: :collection,
          action: :tracked_objects_kinds,
          as: :tracked_objects_kinds

      get "tracked_objects/:kind",
          on: :collection,
          action: :tracked_objects,
          as: :tracked_objects

      post "enqueue_scan_job/:kind",
           on: :collection,
           action: :enqueue_scan_job,
           as: :enqueue_scan_job

      post "backfill_scan_job/:kind",
           on: :collection,
           action: :backfill_scan_job,
           as: :backfill_scan_job
    end

    resources :posts, only: [], controller: "domain/posts", path: "" do
      get :favorites, on: :collection, action: :user_favorite_posts
      get :posts, on: :collection, action: :user_created_posts
    end
  end

  resources :posts,
            as: :domain_posts,
            only: %i[index show],
            controller: "domain/posts" do
    collection do
      # show the search page to find similar posts
      get :visual_search
      # display visually similar posts results
      post :visual_search,
           as: :visual_results,
           to: "domain/posts#visual_results"
    end
    resources :users,
              only: %i[],
              controller: "domain/users",
              path: "",
              constraints: {
                id: id_constraint,
              } do
      get :faved_by, on: :collection, action: :users_faving_post
    end
  end

  resources :post_groups,
            as: :domain_post_groups,
            path: "pools",
            only: %i[],
            controller: "domain/post_groups" do
    resources :posts, only: %i[], controller: "domain/posts" do
      get "/", on: :collection, action: :posts_in_group
    end
  end

  resources :blobs, controller: :blob_entries, only: [:show], param: :sha256

  authenticate :user, ->(user) { user.admin? } do
    # IP address roles management
    namespace :state do
      resources :ip_address_roles, except: [:show] do
        patch :toggle, on: :member
      end
    end

    resources :global_states, path: "state" do
      collection do
        get "fa-cookies", to: "global_states#fa_cookies"
        get "fa-cookies/edit", to: "global_states#edit_fa_cookies"
        patch "fa-cookies", to: "global_states#update_fa_cookies"

        get "ib-cookies", to: "global_states#ib_cookies"
        get "ib-cookies/edit", to: "global_states#edit_ib_cookies"
        patch "ib-cookies", to: "global_states#update_ib_cookies"
      end
    end

    mount GoodJob::Engine => "jobs"
    mount PgHero::Engine => "pghero"

    match "grafana(/*path)", to: "admin/proxy#grafana", via: :all, as: :grafana
    match "prometheus(/*path)",
          to: "admin/proxy#prometheus",
          via: :all,
          as: :prometheus

    resources :log_entries, only: %i[index show] do
      get :stats, on: :collection

      get "filter/*filter",
          on: :collection,
          action: :index,
          constraints: {
            filter: /.*/,
          }
    end
  end
end
