require 'oath/constraints/signed_in'
require 'sidekiq/web'

Rails.application.routes.draw do
  AVAILABLE_LOCALES_REGEX = /#{I18n.available_locales.join("|")}/

  concern :confirmable do
    resources :video_release_confirmations, only: [:new, :create, :destroy]
  end
  concern :contractable do
    resource :contracts, only: [:show]
  end
  concern :notable do
    resources :notes, only: [:index, :new, :create]
  end
  concern :photoable do
    resource :photos, only: [:edit, :update]
  end
  concern :taggable do
    resources :acts_as_taggable_on_tags, only: [:new, :create, :destroy], controller: "tags"
  end
  concern :file_infoable do
    resource :file_infos, only: [:edit, :update]
  end

  constraints AdminSignedInConstraint.new do
    namespace :admin do
      mount Sidekiq::Web => '/background_queue', as: :background_queue

      resources :accounts, only: [:index, :new, :create, :edit, :update, :show]
      resources :users, only: [:index, :new, :create, :edit, :update, :destroy] do
        resource :masquerade, only: :create
      end

      root to: "accounts#index", as: :signed_in_root
    end
  end

  namespace :admin do
    resource :masquerade, only: :destroy
  end

  scope "(:locale)", locale: AVAILABLE_LOCALES_REGEX do
    resource :account_session, only: [:update]
    resource :session, only: [:destroy]
    resource :account, only: [:new, :create, :update]
    resources :account_auths, only: [:index, :create, :update, :destroy]
    resources :projects, shallow: true do
      resources :acquired_media_releases, except: [:show], concerns: [:contractable, :notable, :file_infoable]
      resources :appearance_releases, except: [:show], concerns: [:contractable, :notable]
      resources :appearance_release_imports, only: [:create]
      resources :location_releases, except: [:show], concerns: [:contractable, :notable, :photoable]
      resources :material_releases, except: [:show], concerns: [:contractable, :notable, :photoable]
      resources :music_releases, except: [:show], concerns: [:contractable, :notable]
      resources :talent_releases, except: [:show], concerns: [:contractable, :notable, :photoable]
      resources :medical_releases, except: [:show], concerns: [:contractable, :notable, :photoable]
      resources :contract_templates, only: [:index, :new, :create, :destroy] do
        resource :qr_codes, only: [:show], controller: "contract_templates/qr_codes"
        resource :blank_contracts, only: [:show, :new, :create], controller: "contract_templates/blank_contracts"
      end
      resource :release_template_imports, only: [:new, :create]
      resources :project_memberships, only: [:index, :create, :destroy]
      resources :reports, only: [:index]
      resource :contract_downloads, only: [:create]
      resources :downloads, only: [:index, :destroy]
      resource :report_downloads, only: [:create]
      resources :videos, only: [:index, :new, :create, :edit, :update] do
        collection do
          get :landing
        end
        resources :bookmarks, only: [:new, :create, :destroy, :edit, :update]
        resource :video_reports, only: [:show]
        resource :graphic_reports, only: [:show]
        resource :audio_reports, only: [:show]
        resource :issues_and_concerns_reports, only: [:show]
        resource :report_publications, only: [:create, :destroy], controller: "videos/report_publications"
        resource :video_analyses, only: [:create, :show] do
          resources :audio_confirmations, only: [:new, :create, :destroy], controller: "video_analyses/audio_confirmations"
          resources :unreleased_appearances, only: [:new, :create, :edit, :update, :destroy], controller: "video_analyses/unreleased_appearances"
          resources :edl_events, only: [:create], controller: "video_analyses/edl_events"
          resources :graphics_elements, only: [:new, :create, :edit, :update, :destroy], controller: "video_analyses/graphics_elements"
          resources :talent_releases, only: [:index], controller: "video_analyses/talent_releases"
          resources :appearance_releases, only: [:index], controller: "video_analyses/appearance_releases"
          resources :location_releases, only: [:index], controller: "video_analyses/location_releases"
          resources :music_releases, only: [:index], controller: "video_analyses/music_releases"
          resources :acquired_media_releases, only: [:index], controller: "video_analyses/acquired_media_releases"
          resources :material_releases, only: [:index], controller: "video_analyses/material_releases"
        end
      end
    end
    resources :projects, only: [] do
      resources :broadcasts, except: [:edit] do
        resource :zoom_meeting, only: [:show]
      end
      resources :directories, except: [:index] do
        member do
          get :new_file
          delete :destroy_file
        end
      end
    end
    resource :profile, only: [:show, :update]
    resources :videos, only: [] do
      resources :talent_releases, only: [], concerns: :confirmable
      resources :appearance_releases, only: [], concerns: :confirmable
      resources :location_releases, only: [], concerns: :confirmable
      resources :acquired_media_releases, only: [], concerns: :confirmable
      resources :music_releases, only: [], concerns: :confirmable
      resources :material_releases, only: [], concerns: :confirmable
    end

    scope module: :public do
      resources :accounts, only: [] do
        resources :projects, only: [] do
          resources :contract_templates, only: [] do
            resources :talent_releases, only: [:new, :create]
            resources :appearance_releases, only: [:new, :create]
            resources :acquired_media_releases, only: [:new, :create]
            resources :location_releases, only: [:new, :create]
            resources :material_releases, only: [:new, :create]
            resources :medical_releases, only: [:new, :create]
          end
        end
      end
      resources :broadcasts, param: :token, only: [:show, :update] do
        resource :zoom_meeting, only: [:show]
      end
    end

    RELEASES = [:acquired_media_releases, :appearance_releases, :talent_releases, :material_releases, :location_releases]
    ALL_RELEASES = RELEASES + [:music_releases, :medical_releases]

    ALL_RELEASES.each do |release|
      resources release, only: [], concerns: :taggable
    end

    resources :bulk_taggings, only: [:new, :create]

    namespace :api do
      scope 'v1' do
        get 'sync' => 'sync#index'
        post 'user_token' => 'user_token#create'
        resource :profiles, only: [:show]
        resources :projects, only: [:index] do
          resources :broadcasts, only: [:index, :show, :update]
          RELEASES.each do |release|
            resources release, only: [:index]
          end
          resources :contract_templates, only: [:index]
        end
        resources :contract_templates, only: [:show] do
          RELEASES.each do |release|
            resources release, controller: release, only: [:create]
          end
        end

        (RELEASES - [:appearance_releases]).each do |release|
          resources release, only: [:show, :update] do
            resources :notes, controller: "notes", only: [:create, :index]
          end
        end

        resources :appearance_releases, only: [:show] do
          resources :notes, controller: "notes", only: [:create, :index]
        end
        resources :direct_uploads, only: [:create]
      end
    end

    # TODO: what's the format on this? JSON only?
    resources :notifications, only: [:create]
    resources :stream_notifications, only: [:create]
    resources :zoom_notifications, only: [:create]
    get '/multipart_signatures', to: 'multipart_signatures#create', as: :multipart_signatures

    get "cookies_disabled" => 'pages#show', id: "cookies_disabled", as: :cookies_disabled
    get "accountless_user" => 'pages#show', id: "accountless_user", as: :accountless_user

    resource :session, only: [:new, :create]
    resources :password_resets, only: [:new, :create, :edit, :update]

    root to: 'projects#index', as: :signed_in_root
  end
end
