<?php

namespace App\Http\Controllers;

use App\Models\Equipment;
use Illuminate\Http\Request;

class EquipmentController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $equipments = Equipment::orderBy('created_at', 'desc')->with('user', 'department', 'product')
            ->whereDeletedAt(null)->paginate(10);
        return view('equipment.index', compact('equipments'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'product_id' => 'required|exists:products,id',
            'user_id' => 'required|exists:users,id',
            'department_id' => 'required|exists:departments,id',
            'serial_number' => 'nullable|string|max:255',
            'brand' => 'required|string|max:255',
        ]);

        $validated['user_id'] = Auth::id();
        $equipment = Equipment::create($validated);
        return redirect()->route('equipment.index')->with('success', 'Équipement créé avec succès');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}