<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Machine;
use App\Models\MachineType;

class HardwareSetupController extends Controller
{
    public function selection()
    {
        $machineTypes = MachineType::all();
        return view('admin.hardware_setup.selection', compact('machineTypes'));
    }

    public function manager(Request $request)
    {
        $machineTypeId = $request->input('machine_type_id');
        
        $activeCompanyId = session('active_company_id');
        $machinesQuery = Machine::where('company_id', $activeCompanyId);
        
        if ($machineTypeId) {
            $machinesQuery->where('machine_type_id', $machineTypeId);
        }
        $machines = $machinesQuery->get();
        
        $machineId = $request->input('machine_id');
        $selectedMachine = null;
        if ($machineId) {
            $selectedMachine = $machines->firstWhere('id', $machineId);
        }

        return view('admin.hardware_setup.manager', compact('machines', 'selectedMachine', 'machineTypeId'));
    }
}
