Rails.application.routes.draw do
  resources :nico_tags, path: 'tags/nico', only: [:index, :update]

  scope 'tags/:parent_id/children', controller: :tag_children do
    post ':child_id', action: :create
    delete ':child_id', action: :destroy
  end

  resources :tags, only: [:index, :show] do
    collection do
      get :autocomplete
      get :'with-depth', action: :with_depth
      get :versions, to: 'tag_versions#index'

      scope :name do
        get ':name/deerjikists', action: :deerjikists_by_name
        get ':name/materials', action: :materials_by_name
        get ':name', action: :show_by_name
      end
    end

    member do
      put '', action: :update_all
      patch '', action: :update

      get :deerjikists
      put :deerjikists, action: :update_deerjikists
    end
  end

  scope :preview, controller: :preview do
    get :title
    get :thumbnail
  end

  resources :wiki_pages, path: 'wiki', only: [:index, :show, :create, :update] do
    collection do
      get :search
      get :changes

      scope :title do
        get ':title/exists', action: :exists_by_title
        get ':title', action: :show_by_title
      end
    end

    member do
      get :exists
      get :diff
    end
  end

  resources :posts, only: [:index, :show, :create, :update] do
    collection do
      get :metadata
      post :bulk
      get :random
      get :changes
      get :versions, to: 'post_versions#index'
    end

    member do
      post :viewed
      delete :viewed, action: :unviewed
    end
  end

  namespace :gekanator do
    resources :games, only: [:create], controller: '/gekanator_games' do
      member do
        get :extra_questions
        post :extra_question_answers
      end
    end
    resources :posts, only: [:index], controller: '/gekanator_posts'
    resources :questions, only: [:index], controller: '/gekanator_questions'
    resources :question_suggestions,
              only: [:create],
              controller: '/gekanator_question_suggestions' do
      member do
        post :ai_convert
      end
    end
  end

  get 'users/settings', to: 'user_settings#show'
  patch 'users/settings', to: 'user_settings#update'
  get 'users/theme_slots', to: 'user_theme_slots#index'
  put 'users/theme_slots/:base_theme/:slot_no', to: 'user_theme_slots#update'

  resources :users, only: [:create, :update] do
    collection do
      post :verify
      get :me
      post 'code/renew', action: :renew
    end
  end

  resources :deerjikists, only: [] do
    collection do
      scope ':platform/:code' do
        get '', action: :show
        put '', action: :update
        delete '', action: :destroy
      end
    end
  end

  resources :theatres, only: [:show] do
    member do
      put :watching
      patch :next_post
      put :skip_vote
      delete :skip_vote, action: :unskip_vote
      get :post_selection_weights
    end

    resources :comments, controller: :theatre_comments, only: [:index, :create, :destroy]
    resources :programmes, controller: :theatre_programmes, only: [:index]
    resources :skip_events, controller: :theatre_skip_events, only: [:index]
  end

  get 'materials/download.zip', to: 'materials#download'
  get 'materials/versions', to: 'material_versions#index'
  resources :material_sync_suppressions, path: 'materials/suppressions', only: [:index, :create]
  resources :materials, only: [:index, :show, :create, :update, :destroy]
end
