# frozen_string_literal: true

require_relative 'base_controller'
require_relative '../models/benchmark'

# /benchmark routes
class BenchmarkController < BaseController

  get '/benchmark' do
    redirect('/benchmark/list')
  end

  get '/benchmark/list' do
    benchmarks = Benchmark.reverse(:updated_at).limit(10).all()

    erb :'benchmark/index', locals: {
      title: 'List of Benchmarks',
      benchmarks: benchmarks
    }
  end

  get '/benchmark/add' do
    erb :'benchmark/add', locals: {
      title: 'Add Benchmark'
    }
  end
  post '/benchmark/add' do
    benchmark = Benchmark.create(
      name: params[:benchmark_name],
      scoring: params[:benchmark_scoring],
      description: params[:benchmark_description]
    )

    redirect "/benchmark/#{benchmark.id}"
  end

  get '/benchmark/:benchmark_id' do
    benchmark = Benchmark.where(id: params[:benchmark_id]).first()
    erb :'benchmark/view', locals: {
      title: benchmark.name,
      benchmark: benchmark
    }
  end

  get '/benchmark/:benchmark_id/edit' do
    benchmark = Benchmark.where(id: params[:benchmark_id]).first()
    erb :'benchmark/edit', locals: {
      title: "Editing: #{benchmark.name}",
      benchmark: benchmark
    }
  end

  post '/benchmark/:benchmark_id/edit' do
    benchmark = Benchmark.where(id: params[:benchmark_id]).first()

    benchmark.update(
      name: params[:benchmark_name],
      scoring: params[:benchmark_scoring],
      description: params[:benchmark_description]
    )

    redirect "/benchmark/#{benchmark.id}"
  end

end
