Rails.application.routes.draw do
  resource :account do
    resource :join_code, module: :accounts

    scope module: :accounts do
      resource :settings
      resource :entropy_configuration
    end
  end

  resources :users do
    resource :role, module: :users
  end

  resources :collections do
    scope module: :collections do
      resource :subscriptions
      resource :workflow, only: :update
      resource :involvement
      resource :publication
    end

    resources :cards
  end

  namespace :cards do
    resources :previews
    resources :drops
  end

  resources :cards do
    scope module: :cards do
      resource :engagement
      resource :goldness
      resource :image
      resource :pin
      resource :closure
      resource :publish
      resource :reading
      resource :recover
      resource :staging
      resource :watch
      resource :collection, only: :update

      resources :assignments
      resources :taggings

      resources :comments do
        resources :reactions, module: :comments
      end
    end
  end

  resources :notifications do
    scope module: :notifications do
      get "tray", to: "trays#show", on: :collection
      get "settings", to: "settings#show", on: :collection

      post "readings", to: "readings#create_all", on: :collection, as: :read_all
      post "reading", to: "readings#create", on: :member, as: :read
    end
  end

  resource :search
  namespace :searches do
    resources :queries
  end

  resources :filters

  resources :events, only: :index
  namespace :events do
    resources :days
  end

  resources :workflows do
    resources :stages, module: :workflows
  end

  resources :uploads, only: :create
  get "/u/*slug" => "uploads#show", as: :upload

  resources :qr_codes
  get "join/:join_code", to: "users#new", as: :join
  post "join/:join_code", to: "users#create"

  resource :session do
    scope module: "sessions" do
      resources :transfers, only: %i[ show update ]
    end
    resource :launchpad, only: %i[ show update ], controller: "sessions/launchpad"
  end

  namespace :signup do
    get "/" => "accounts#new"
    resources :accounts, only: %i[ new create ]
    get "/session" => "sessions#create" # redirect from Launchpad after mid-signup authentication
    resources :completions, only: %i[ new create ]
  end

  resources :users do
    scope module: :users do
      resource :avatar
    end
  end

  resources :commands do
    scope module: :commands do
      resource :undo, only: :create
    end
  end

  namespace :my do
    resources :pins
  end

  namespace :prompts do
    resources :cards
    resources :collections do
      scope module: :collections do
        resources :users
      end
    end
  end

  namespace :public do
    resources :collections do
      scope module: :collections do
        resources :card_previews
      end
    end
  end

  direct :published_collection do |collection, options|
    route_for :public_collection, collection.publication.key
  end

  resolve "Card" do |card, options|
    route_for :collection_card, card.collection, card, options
  end

  resolve "Comment" do |comment, options|
    options[:anchor] = ActionView::RecordIdentifier.dom_id(comment)
    route_for :collection_card, comment.card.collection, comment.card, options
  end

  resolve "Mention" do |mention, options|
    polymorphic_path(mention.source, options)
  end

  resolve "Notification" do |notification, options|
    polymorphic_path(notification.notifiable_target, options)
  end

  resolve "Event" do |event, options|
    polymorphic_path(event.target, options)
  end

  get "up", to: "rails/health#show", as: :rails_health_check
  get "manifest" => "rails/pwa#manifest", as: :pwa_manifest

  root "events#index"

  Queenbee.routes(self)

  namespace :admin do
    mount MissionControl::Jobs::Engine, at: "/jobs"
  end
end
