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 :regia_link
          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
    get "login", to: "auth#new", as: :login
    post "login", to: "auth#create"
    delete "logout", to: "auth#destroy", as: :logout
    resource :password, only: %i[edit update], controller: "passwords"

    root to: "dashboard#index"
    get "metrics", to: "dashboard#metrics"
    get "billing", to: "billing#index", as: :billing
    post "billing/payments/:payment_id/attach_pdf", to: "billing#attach_pdf", as: :billing_payment_attach_pdf
    resources :teams, only: %i[index show]
    resources :clubs, only: [] do
      resources :billing_invoices, only: %i[index new create edit update], controller: "billing_invoices"
    end
    resources :sessions, only: %i[index show] do
      member { post :stop }
    end
  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

  get "regia/:token", to: "public/regia#show", as: :public_regia
  get "regia/:token/status.json", to: "public/regia#status", as: :public_regia_status
  patch "regia/:token/score", to: "public/regia#score", as: :public_regia_score
  post "regia/:token/pause", to: "public/regia#pause", as: :public_regia_pause
  post "regia/:token/stop", to: "public/regia#stop", as: :public_regia_stop

  root to: "public/pages#home"
  get "sitemap.xml", to: "public/sitemap#show", defaults: { format: :xml }

  scope module: :public, as: :public do
    get "funzionalita", to: "pages#features", as: :features
    get "faq", to: "pages#faq", as: :faq
    get "pallavolo-giovanile", to: "pages#pallavolo", as: :pallavolo
    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 "password/forgot", to: "password_resets#new", as: :password_forgot
    post "password/forgot", to: "password_resets#create"
    get "password/reset", to: "password_resets#edit", as: :password_reset
    patch "password/reset", to: "password_resets#update"
    get "clubs/new", to: "clubs#new", as: :new_club
    post "clubs", to: "clubs#create"
    get "clubs/:id", to: "clubs#show", as: :club
    get "clubs/:id/edit", to: "clubs#edit", as: :edit_club
    patch "clubs/:id", to: "clubs#update"
    get "clubs/:id/billing", to: "clubs#billing", as: :club_billing
    get "clubs/:id/billing/profile", to: "club_billing#profile", as: :club_billing_profile
    patch "clubs/:id/billing/profile", to: "club_billing#update_profile"
    post "clubs/:id/billing/cancel_subscription", to: "club_billing#cancel_subscription", as: :club_billing_cancel_subscription
    get "clubs/:id/billing/invoices/:invoice_id", to: "club_billing#download_invoice", as: :club_billing_invoice
    get "clubs/:id/checkout", to: "clubs#checkout", as: :club_checkout
    post "clubs/:id/portal", to: "clubs#portal", as: :club_portal
    get "clubs/:club_id/matches/new", to: "club_matches#new", as: :new_club_match
    post "clubs/:club_id/matches", to: "club_matches#create", as: :club_matches
    get "teams/new", to: redirect("/clubs/new")
    get "clubs/:club_id/teams/new", to: "teams#new", as: :new_club_team
    post "clubs/:club_id/teams", to: "teams#create", as: :club_teams
    get "teams/:id/dettagli", to: "teams#details", as: :team_details
    get "teams/:id/dashboard", to: "teams#dashboard"
    get "teams/:id/roster", to: "teams#roster"
    get "teams/:id/edit", to: "teams#edit", as: :edit_team
    patch "teams/:id", to: "teams#update", as: :team
    resources :teams, only: [] do
      resources :roster_members, only: %i[create edit update destroy], controller: "team_roster_members"
      resources :matches, only: %i[index new create edit update destroy], controller: "matches"
    end
    get "teams/:id/invite", to: "teams#invite", as: :team_invite
    post "teams/:id/invite", to: "teams#create_invitation"
    post "teams/:id/staff/self", to: "teams#assign_self_staff", as: :team_assign_self_staff
    delete "teams/:id/staff/self", to: "teams#clear_self_staff", as: :team_clear_self_staff
    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
