# frozen_string_literal: true

require_relative 'base_controller'
require_relative '../models/result'

# /result routes
class ResultController < BaseController

  post '/result/add' do
    result_minimum = params[:result_minimum] if params.key?(:result_minimum)
    result_maximum = params[:result_maximum] if params.key?(:result_maximum)

    Result.create(
      test_id: params[:result_test],
      benchmark_profile_id: params[:result_benchmark],
      avg_score: params[:result_average],
      min_score: result_minimum,
      max_score: result_maximum
    )

    if params.key?(:result_referrer)
      if params[:result_referrer] == 'test'
        redirect "/test/#{params[:result_test]}"
      elsif params[:result_referrer] == 'benchmark'
        redirect "/benchmark/#{params[:result_benchmark]}"
      end
    else
      redirect '/result'
    end
  end

end
