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

  Healthcheck.routes(self)
  get 'admin/status'

  # Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
  # get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
  # get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
  get 'admin/screenshots'

  get 'admin/integration'

  # https://www.rubydoc.info/github/plataformatec/devise/ActionDispatch%2FRouting%2FMapper:devise_for
  devise_for :users, controllers: {
    # registrations: "users/registrations",
    # passwords: "users/passwords",
    omniauth_callbacks: 'users/omniauth_callbacks',
    sessions: 'users/my_sessions'
  }

  get 'packages' => 'packages#grid', as: :packages_grid
  get 'packages/list' => 'packages#list', as: :packages_list
  get 'packages?show=without' => 'packages#grid?show=without', as: :packages_without
  get 'moderate' => 'moderate#index'
  get 'admin/logs' => 'admin#logs'
  get 'my/profile'
  get 'my/uploads'
  get 'my/welcome'
  get 'my/screenshots'
  get 'my/moderate_list', as: :moderate_list
  get 'my/logs', as: :logs
  get 'package/:name' => 'packages#details', as: :package, name: %r{[^/]+}
  # get 'package_reviews/:name' => 'packages#reviews', as: :package_reviews, name: /[^\/]+/
  get 'upload', to: redirect('/packages'), as: :upload_legacy # legacy upload form
  post 'uploadfile' => 'packages#legacy_uploadfile', name: %r{[^/]+}
  get 'upload/:name' => 'packages#upload', as: :upload, name: %r{[^/]+}
  get 'upload/:name/json' => 'packages#upload', as: :upload_json, name: %r{[^/]+}, returns: :json
  post 'upload/:name' => 'packages#upload_receive', as: :upload_receive, name: %r{[^/]+}
  post 'upload/:name/json' => 'packages#upload_receive', as: :upload_receive_json, name: %r{[^/]+},
       returns: :json
  # TODO: "get" is probably the wrong method to delete a screenshot
  get 'delete_screenshot/:id' => 'packages#delete_screenshot', as: :delete_screenshot
  get 'hide_screenshot/:id' => 'packages#hide_screenshot', as: :hide_screenshot
  get 'unhide_screenshot/:id' => 'packages#unhide_screenshot', as: :unhide_screenshot
  patch 'update_screenshot_description/:id' => 'packages#update_screenshot_description',
        as: :update_screenshot_description
  # post 'report_screenshot/:id' => 'packages#report_screenshot', as: :report_screenshot
  # TODO: "get" is probably the wrong method to delete a screenshot
  get 'approve_screenshot/:id' => 'packages#approve_screenshot', as: :approve_screenshot
  get 'about' => 'welcome#about'

  get 'thumbnail/:name' => 'packages#send_image', as: :thumbnail_image, name: %r{[^/]+},
      defaults: { size: :thumb }
  # Return thumbnail by specific screenshot_id
  get 'thumbnail/:name/:screenshot_id' => 'packages#send_image', as: :thumbnail_with_id, name: %r{[^/]+},
      defaults: { size: :thumb }
  get 'thumbnail-404/:name' => 'packages#send_image', name: %r{[^/]+}, defaults: { size: :thumb }
  get 'thumbnail-with-version/:name/:version' => 'packages#send_image', name: %r{[^/]+},
      version: /\d.*/, as: :thumbnail_with_version, defaults: { size: :thumb }

  # Return small image for a specific package
  get 'small/:name' => 'packages#send_image', as: :small_image, name: %r{[^/]+},
      defaults: { size: :small }
  # Return small image by specific screenshot_id
  get 'small/:name/:screenshot_id' => 'packages#send_image', as: :small_image_with_id, name: %r{[^/]+},
      defaults: { size: :small }

  # Return large image for a specific package
  get 'screenshot/:name' => 'packages#send_image', as: :screenshot_image, name: %r{[^/]+},
      defaults: { size: :large }
  # Return large image by specific screenshot_id
  get 'screenshot/:name/:screenshot_id' => 'packages#send_image', as: :screenshot_image_with_id, name: %r{[^/]+},
      defaults: { size: :large }
  get 'screenshot-404/:name' => 'packages#send_image', name: %r{[^/]+}
  get 'screenshot-with-version/:name/:version' => 'packages#send_image', name: %r{[^/]+},
      version: /\d.*/, defaults: { size: :large }

  # Legacy URLs
  get 'with_screenshots', to: redirect('/packages?show=with')
  get 'without_screenshots', to: redirect('/packages?show=without')

  # JSON data responses
  get 'json/package/:name' => 'json#package', as: :json_package, defaults: { format: :json }
  get 'json/packages' => 'json#packages', as: :json_packages, defaults: { format: :json }
  get 'json/screenshots' => 'json#screenshots', as: :json_screenshots, defaults: { format: :json }
  get 'json/packages-without-screenshots' => 'json#packages_without_screenshots',
      defaults: { format: :json }

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'welcome#home'
  # root to: 'packages#home'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end

  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
