Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
  # Can be used by load balancers and uptime monitors to verify that the app is live.
  # get "up" => "rails/health#show", as: :rails_health_check

  get 'admin', to: redirect('/admin/en')

  namespace :admin do
    scope ':locale', constraints: { locale: /en|zh|hr|cs|da|nl|fi|fr|fr-CA|de|hu|it|ja|ko|nb|pl|pt|ro|sr|sk|sl|es|sv|uk/ } do

      # Assets
      resources :assets do
        collection do
          post :upload
        end
      end

      # Nodes
      resources :nodes do
        member do
          get :children
          get :tree
        end
        collection do
          get :tree
          patch :sort
          patch :toggle
        end
        resources :assets, only: [ :index ]
        resources :attachments, only: [ :new ]
      end

      # Users
      resources :users

      # Root
      root to: 'nodes#index'
    end

    # Cache
    delete 'cache/clear', to: "admin#clear_cache"

    resources :sessions, path: 'login', only: [:index, :create, :destroy]
    get "logout", to: 'sessions#destroy', as: "logout"

    # 2 step auth
    post 'login/verify', to: 'sessions#verify'
    get  'login/verification', to: 'sessions#verification'
  end

  scope ':locale', constraints: { locale: /en|zh|hr|cs|da|nl|fi|fr|fr-CA|de|hu|it|ja|ko|nb|pl|pt|ro|sr|sk|sl|es|sv|uk/ } do

    get 'player', to: 'players#new'
    post 'player', to: 'players#create'

    post 'q/:id/answer', to: 'answers#create'
    patch 'q/:id/answer', to: 'answers#update'
    get 'q/:id/answer', to: 'questions#answer'
    get 'q/:id', to: 'questions#show'

    post 'score', to: 'questions#score'
    get 'result/:id', to: 'questions#shared_result'
    get 'result', to: 'questions#result'

    get '', to: 'languages#show'
    # get '*url', to: 'site#page', constraints: lambda { |req| req.path.exclude?('storage') }
  end

  put 'update_locale', to: 'languages#update'


  # Defines the root path route ("/")
  root "languages#index"
end
