<commit_before># frozen_string_literal: true

require 'sidekiq/web'

NolotiroOrg::Application.routes.draw do
  ActiveAdmin.routes(self)

  scope '/api' do
    scope '/v1' do
      get '/ad/:id', format: 'json', to: 'api/v1#ad_show', as: 'apiv1_ad_show'
      get '/woeid/:id/:type', format: 'json', to: 'api/v1#woeid_show', as: 'apiv1_woeid_show'
      get '/woeid/list', format: 'json', to: 'api/v1#woeid_list', as: 'apiv1_woeid_list'
    end
  end

  devise_for :users, only: :omniauth_callbacks, controllers: { omniauth_callbacks: 'callbacks' }

  get '/locales/:locale', to: 'locales#show', as: :locale

  # i18n
  scope '(:locale)', locale: /#{I18n.available_locales.join("|")}/ do
    root 'woeid#show', defaults: { type: 'give' }

    # FIXME: type on ads#create instead of params
    # FIXME: nolotirov2 legacy - redirect from /es/ad/create
    resources :ads, path: 'ad', path_names: { new: 'create' }, except: :index do
      resources :comments, only: :create
    end

    constraints(AdConstraint.new) do
      scope '/ad' do
        get '/:id/:slug', to: 'ads#show', as: 'adslug'
        get '/edit/id/:id', to: 'ads#edit', as: 'ads_edit'
        post '/bump/id/:id', to: 'ads#bump', as: 'ads_bump'
        get '/listall/ad_type/:type(/status/:status)(/page/:page)',
            to: 'woeid#show',
            as: 'ads_listall'
        get '/listuser/id/:id(/type/:type)(/status/:status)(/page/:page)',
            to: 'users#listads',
            as: 'listads_user'
      end

      # locations lists
      get '/woeid/:id/:type(/status/:status)(/page/:page)',
          to: 'woeid#show',
          as: 'ads_woeid',
          constraints: { id: /\d+/ }
    end

    # location change
    scope '/location' do
      get  '/change', to: 'location#ask', as: 'location_ask'
      get  '/change2', to: 'location#change', as: 'location_change'
      post '/change', to: 'location#list'
      post '/change2', to: 'location#change'
    end

    devise_for :users,
               skip: [:omniauth_callbacks, :unlocks],
               controllers: { registrations: 'registrations' },
               path: 'user',
               path_names: {
                 sign_up: 'register',
                 sign_in: 'login',
                 sign_out: 'logout',
                 password: '<PASSWORD>'
               }

    # friendships
    resources :friendships, only: [:create, :destroy]

    scope '/admin' do
      authenticate :user, ->(u) { u.admin? } do
        mount Sidekiq::Web, at: '/jobs'
      end
    end

    get '/user/edit/id/:id', to: redirect('/es/user/edit'), as: 'user_edit'
    get '/profile/:id', to: 'users#profile', as: 'profile'

    # search
    get '/search', to: redirect { |params, request|
      raw_query = URI.escape(request.query_string)
      query = Rack::Utils.parse_query(raw_query).symbolize_keys

      woeid = query[:woeid] || query[:woeid_code]
      new_path = "#{params[:locale]}/woeid/#{woeid}"

      type = if query[:ad_type].present?
               query[:ad_type] == '2' ? 'want' : 'give'
             elsif query[:type].present?
               query[:type]
             else
               'give'
             end

      new_path = "#{new_path}/#{type}"

      new_path = "#{new_path}/status/#{query[:status]}" if query[:status].present?
      new_path = "#{new_path}/page/#{query[:page]}" if query[:page].present?
      "#{new_path}?q=#{query[:q]}"
    }

    # blocking
    resources :blockings, only: [:create, :destroy]

    # messaging
    resources :conversations, path: '/messages/' do
      member { delete 'trash' }
      collection { delete 'trash' }
    end

    # rss
    # nolotirov2 - legacy
    # FIX: las URLs legacy vienen asi
    # /en                     /rss/feed/woeid/766273                     /ad_type/give
    # Lo solucionamos en el nginx.conf y una configuracion para hacer el search and replace
    # http://stackoverflow.com/questions/22421522/nginx-rewrite-rule-for-replacing-space-whitespace-with-hyphen-and-convert-url-to<commit_msg>Remove complexity from routes file<commit_after># frozen_string_literal: true

require 'sidekiq/web'

NolotiroOrg::Application.routes.draw do
  ActiveAdmin.routes(self)

  scope '/api' do
    scope '/v1' do
      get '/ad/:id', format: 'json', to: 'api/v1#ad_show', as: 'apiv1_ad_show'
      get '/woeid/:id/:type', format: 'json', to: 'api/v1#woeid_show', as: 'apiv1_woeid_show'
      get '/woeid/list', format: 'json', to: 'api/v1#woeid_list', as: 'apiv1_woeid_list'
    end
  end

  devise_for :users, only: :omniauth_callbacks, controllers: { omniauth_callbacks: 'callbacks' }

  get '/locales/:locale', to: 'locales#show', as: :locale

  # i18n
  scope '(:locale)', locale: /#{I18n.available_locales.join("|")}/ do
    root 'woeid#show', defaults: { type: 'give' }

    # FIXME: type on ads#create instead of params
    # FIXME: nolotirov2 legacy - redirect from /es/ad/create
    resources :ads, path: 'ad', path_names: { new: 'create' }, except: :index do
      resources :comments, only: :create
    end

    constraints(AdConstraint.new) do
      scope '/ad' do
        get '/:id/:slug', to: 'ads#show', as: 'adslug'
        get '/edit/id/:id', to: 'ads#edit', as: 'ads_edit'
        post '/bump/id/:id', to: 'ads#bump', as: 'ads_bump'
        get '/listall/ad_type/:type(/status/:status)(/page/:page)',
            to: 'woeid#show',
            as: 'ads_listall'
        get '/listuser/id/:id(/type/:type)(/status/:status)(/page/:page)',
            to: 'users#listads',
            as: 'listads_user'
      end

      # locations lists
      get '/woeid/:id/:type(/status/:status)(/page/:page)',
          to: 'woeid#show',
          as: 'ads_woeid',
          constraints: { id: /\d+/ }
    end

    # location change
    scope '/location' do
      get  '/change', to: 'location#ask', as: 'location_ask'
      get  '/change2', to: 'location#change', as: 'location_change'
      post '/change', to: 'location#list'
      post '/change2', to: 'location#change'
    end

    devise_for :users,
               skip: [:omniauth_callbacks, :unlocks],
               controllers: { registrations: 'registrations' },
               path: 'user',
               path_names: {
                 sign_up: 'register',
                 sign_in: 'login',
                 sign_out: 'logout',
                 password: '<PASSWORD>'
               }

    # friendships
    resources :friendships, only: [:create, :destroy]

    scope '/admin' do
      authenticate :user, ->(u) { u.admin? } do
        mount Sidekiq::Web, at: '/jobs'
      end
    end

    get '/user/edit/id/:id', to: redirect('/es/user/edit'), as: 'user_edit'
    get '/profile/:id', to: 'users#profile', as: 'profile'

    def rewritten_woeid(query)
      query[:woeid] || query[:woeid_code]
    end

    def rewritten_type(query)
      if query[:ad_type].present?
        query[:ad_type] == '2' ? 'want' : 'give'
      elsif query[:type].present?
        query[:type]
      else
        'give'
      end
    end

    # search
    get '/search', to: redirect { |params, request|
      raw_query = URI.escape(request.query_string)
      query = Rack::Utils.parse_query(raw_query).symbolize_keys

      new_path = "#{params[:locale]}/woeid/#{rewritten_woeid(query)}"
      new_path = "#{new_path}/#{rewritten_type(query)}"
      new_path = "#{new_path}/status/#{query[:status]}" if query[:status].present?
      new_path = "#{new_path}/page/#{query[:page]}" if query[:page].present?
      "#{new_path}?q=#{query[:q]}"
    }

    # blocking
    resources :blockings, only: [:create, :destroy]

    # messaging
    resources :conversations, path: '/messages/' do
      member { delete 'trash' }
      collection { delete 'trash' }
    end

    # rss
    # nolotirov2 - legacy
    # FIX: las URLs legacy vienen asi
    # /en                     /rss/feed/woeid/766273                     /ad_type/give
    # Lo solucionamos en el nginx.conf y una configuracion para hacer el search and replace
    # http://stackoverflow.com/questions/22421522/nginx-rewrite-rule-for-replacing-space-whitespace-with-hyphen-and-convert-url-to
{'fraction_non_alphanumeric': 0.16032608695652173, 'fraction_numerical': 0.007411067193675889, 'mean_word_length': 2.4338422391857506, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '571639', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2776, 'n_words': 613}