# frozen_string_literal: true

require_relative '../spec_helper'
require_relative '../../src/models/hardware'

RSpec.describe(HardwareController) do
  # GET /hardware - redirects to /hardware/list
  describe 'GET /hardware' do
    before { get '/hardware' }

    it 'Hardware base route is a redirect' do
      expect(last_response).to(be_redirect)
    end

    it 'Hardware base route is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Hardware base route Location header points to /hardware/list' do
      expect(last_response['Location']).to(eq("#{BASE_URL}/hardware/list"))
    end
  end

  # GET /hardware/list - displays a table of hardwares
  describe 'GET /hardware/list' do
    before { get '/hardware/list' }

    it 'Hardware list page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Hardware list page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it "Hardware list page contains 'List of hardware' on page." do
      expect(last_response.body).to(include('List of hardware'))
    end
  end

  # GET /hardware/add - form for adding hardware
  describe 'GET /hardware/add' do
    before { get '/hardware/add' }

    it 'Hardware add page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Hardware add page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it "Hardware add page contains 'Add new hardware' on page." do
      expect(last_response.body).to(include('Add new hardware'))
    end
  end

  # POST /hardware/add - backend for adding a hardware component
  describe 'POST /hardware/add' do
    before do
      request_data = {
        hardware_name: 'Test Hardware',
        hardware_type: 'gpu'
      }
      post '/hardware/add', request_data
    end

    it 'Hardware add POST route is a redirect.' do
      expect(last_response).to(be_redirect)
    end

    it 'Hardware add POST route is an HTML response.' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Hardware add POST route Location header points to /hardware/1' do
      expect(last_response['Location']).to(eq("#{BASE_URL}/hardware/1"))
    end

    it 'Hardware add POST route creates new Hardware.' do
      expect(Hardware.count).to(eq(1))
    end

    it 'Hardware add POST route created hardware has name.' do
      expect(Hardware.first.name).to(eq('Test Hardware'))
    end

    it 'Hardware add POST route created hardware has type.' do
      expect(Hardware.first.type).to(eq('gpu'))
    end
  end

  # GET /hardware/:hardware_id - page for viewing a hardware model
  describe 'GET /hardware/:hardware_id' do
    before do
      @hardware = Hardware.create(name: 'Test Hardware', type: 'gpu')
      get "/hardware/#{@hardware.id}"
    end

    it 'Hardware view page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Hardware view page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Hardware view page contains "Add new hardware" on page.' do
      expect(last_response.body).to(include("#{@hardware.name}"))
    end
  end

  # GET /hardware/:hardware_id/edit - page for editing a hardware model
  describe 'GET /hardware/:hardware_id/edit' do
    before do
      @hardware = Hardware.create(name: 'Test Hardware', type: 'gpu')
      get "/hardware/#{@hardware.id}/edit"
    end

    it 'Hardware edit page returns 200.' do
      expect(last_response).to(be_ok)
    end

    it 'Hardware edit page is an HTML response' do
      expect(last_response['Content-Type']).to(include('text/html'))
    end

    it 'Hardware edit page contains "Editing: <hardware name>" on page.' do
      expect(last_response.body).to(include("Editing: #{@hardware.name}"))
    end
  end
end
