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


  namespace :api do
    namespace :v1 do
      get "leaderboard", to: "leaderboard#index"
    end
  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 "*url", to: "site#page", constraints: lambda { |req| req.path.exclude?("storage") }

    post "start", to: "game#start", as: :start
    get "facts", to: "game#facts"
    get "intro", to: "game#intro"

    get "stage/:id", to: "game#stage"

    post "stage/:id/answer", to: "game#answer"

    get "stage/:id/result", to: "game#stage_result"

    get "last_save", to: "game#last_save"
    post "last_save/answer", to: "game#answer_last_save"

    get "done", to: "game#done"
    get "results", to: "game#results"

    get "", to: "game#index", as: :locale_root
  end

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