require 'sidekiq/web'

Rails.application.routes.draw do
  devise_for :users, controllers: { confirmations: 'users/confirmations' }

  get 'welcome', to: 'welcome#index'
  get 'check_your_email', to: 'welcome#check_your_email'

  get 'signup', to: 'signup#index'
  match 'signup/:step', to: 'signup#steps', as: :signup_steps, via: [:get, :post]
  post 'signup_validate', to: 'signup#validate'

  namespace :contributions do
    root to: 'donations#index'
    get 'projects', to: 'projects#index'
    resources :donations, only: ['index']
  end

  resources :invitations, only: ['index', 'show', 'create', 'destroy']

  namespace :services do
    get 'storage', to: 'remotestorage#dashboard'

    resource :chat, only: [:show], controller: 'chat'

    resources :lightning, only: [:index] do
      collection do
        get 'transactions'
        get 'qr_lnurlp'
      end
    end

    resource :mastodon, only: [:show], controller: 'mastodon'
  end

  resources :settings, param: 'section', only: ['index', 'show', 'update'] do
    collection do
      post 'update_email'
      post 'reset_password'
      post 'set_nostr_pubkey'
      delete 'nostr_pubkey', to: 'settings#remove_nostr_pubkey'
    end
  end

  get 'lnurlpay/:address', to: 'lnurlpay#index',
    as: 'lightning_address', constraints: { address: /[^\/]+/}
  get 'lnurlpay/:address/invoice', to: 'lnurlpay#invoice',
    as: 'lnurlpay_invoice', constraints: { address: /[^\/]+/}
  get 'keysend/:address', to: 'lnurlpay#keysend',
    as: 'lightning_address_keysend', constraints: { address: /[^\/]+/}

  get '.well-known/nostr', to: 'well_known#nostr'

  post 'webhooks/lndhub', to: 'webhooks#lndhub'

  namespace :api do
    get 'kredits/onchain_btc_balance', to: 'kredits#onchain_btc_balance'
  end

  namespace :admin do
    root to: 'dashboard#index'
    resources 'users', param: 'address', only: ['index', 'show'], constraints: { address: /.*/ }
    get 'invitations', to: 'invitations#index'
    resources :donations
    get 'lightning', to: 'lightning#index'

    namespace :settings do
      resources 'registrations', only: ['index', 'create']
      resources 'services', only: ['index', 'create']
    end
  end

  namespace :rs do
    resource :oauth, only: [:new, :create], path_names: {
      new: ':useraddress', create: ':useraddress'
    }, controller: 'oauth', constraints: { useraddress: /[^\/]+/}
    get 'oauth/token/:id/launch_app' => 'oauth#launch_app', as: :launch_app
  end

  get '.well-known/webfinger', to: 'webfinger#show'

  namespace :discourse do
    get "connect", to: 'sso#connect'
  end

  authenticate :user, ->(user) { user.is_admin? } do
    mount Sidekiq::Web, at: '/sidekiq'
    mount Flipper::UI.app(Flipper), at: '/flipper'
  end

  # Letter Opener (open "sent" emails in dev and staging)
  if Rails.env.match(/staging|development/)
    mount LetterOpenerWeb::Engine, at: '/letter_opener'
  end

  root to: 'dashboard#index'
end
