Rails.application.routes.draw do
  mount ActionCable.server => "/cable"

  get "up" => "rails/health#show", as: :rails_health_check

  namespace :api do
    namespace :v1 do
      post "auth/login", to: "auth#login"
      post "auth/register", to: "auth#register"
      post "auth/logout", to: "auth#logout"
      post "auth/refresh", to: "auth#refresh"
      get "auth/me", to: "auth#me"

      resources :teams do
        member do
          post :members, action: :add_member
          delete "members/:user_id", action: :remove_member
          get :recordings
        end
        resources :matches, only: %i[index create]
        get "youtube/authorize", to: "youtube#authorize"
      end

      resources :matches, only: %i[show update destroy] do
        resources :sessions, only: %i[create], controller: "stream_sessions"
      end

      resources :sessions, only: %i[show], controller: "stream_sessions" do
        member do
          patch :start
          patch :stop
          patch :pause
          get :events
          post :telemetry
          post :pairing_token
          post :claim_pairing
          post :network_test
          get :youtube_stats
        end
      end

      get "youtube/callback", to: "youtube#callback"
    end
  end

  namespace :webhooks do
    post "mediamtx/connect", to: "mediamtx#connect"
    post "mediamtx/disconnect", to: "mediamtx#disconnect"
    post "mediamtx/ready", to: "mediamtx#ready"
    post "mediamtx/validate_publish", to: "mediamtx#validate_publish"
    post "stripe", to: "stripe#create"
  end

  post "internal/validate_publish", to: "webhooks/mediamtx#validate_publish"

  namespace :admin do
    root to: "dashboard#index"
    resources :teams, only: %i[index show]
    resources :sessions, only: %i[index show]
  end

  get "hls/*path", to: "hls_proxy#show", format: false, constraints: { path: /.+/ }

  get "live", to: "public/live#index", as: :public_live_index
  get "live/:id", to: "public/live#show", as: :public_live
  get "live/:id/status.json", to: "public/live#status", as: :public_live_status
  get "replay/:id", to: "public/replay#show", as: :public_replay

  root to: "public/pages#home"

  scope module: :public, as: :public do
    get "funzionalita", to: "pages#features", as: :features
    get "prezzi", to: "pages#pricing", as: :prezzi
    get "pricing", to: redirect("/prezzi")
    get "privacy", to: "pages#privacy", as: :privacy
    get "termini", to: "pages#terms", as: :termini
    get "signup", to: "registrations#new"
    post "signup", to: "registrations#create"
    get "login", to: "sessions#new"
    post "login", to: "sessions#create"
    delete "logout", to: "sessions#destroy"
    get "teams/new", to: "teams#new"
    post "teams", to: "teams#create"
    get "teams/:id/dashboard", to: "teams#dashboard", as: :team_dashboard
    get "teams/:id/billing", to: "teams#billing", as: :team_billing
    get "teams/:id/checkout", to: "teams#checkout", as: :team_checkout
    post "teams/:id/portal", to: "teams#portal", as: :team_portal
    get "teams/:id/invite", to: "teams#invite", as: :team_invite
    post "teams/:id/invite", to: "teams#create_invitation"
    delete "teams/:id/members/:user_id", to: "teams#remove_member", as: :team_remove_member
    delete "teams/:id/invitations/:invitation_id", to: "teams#destroy_invitation", as: :team_destroy_invitation
    get "join/:token", to: "invitations#show", as: :invitation
    post "join/:token", to: "invitations#accept"
  end

end
