# frozen_string_literal: true

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

# /hardware routes
class HardwareController < BaseController

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

  get '/hardware/list' do
    hardware = Hardware.reverse(:updated_at).limit(10).all()

    erb :'hardware/index', locals: {
      title: 'List of Hardware',
      hardware: hardware
    }
  end

  get '/hardware/add' do
    erb :'hardware/add', locals: {
      title: 'Add Hardware'
    }
  end
  post '/hardware/add' do
    hardware = Hardware.create(
      name: params[:hardware_name],
      type: params[:hardware_type]
    )

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

  get '/hardware/:hardware_id' do
    benchmarks = Benchmark.order(:name).all()
    hardware = Hardware.where(id: params[:hardware_id]).first()
    erb :'hardware/view', locals: {
      title: hardware.name,
      hardware: hardware,
      benchmarks: benchmarks
    }
  end

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

  post '/hardware/:hardware_id/edit' do
    hardware = Hardware.where(id: params[:hardware_id]).first()

    hardware.update(
      name: params[:hardware_name],
      type: params[:hardware_type]
    )

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

end
