<?php

namespace App\Models;

use Illuminate\Support\Str;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Deligoez\LaravelModelHashId\Traits\HasHashId;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Unit extends Model
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable, HasHashId, HasRoles, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $guarded = [];


    // Produits utilisant cette unité de mesure
    public function products()
    {
        return $this->hasMany(Product::class);
    }

    //Génération automatique de la référence unique pour le département
    protected static function booted()
    {
        static::creating(function ($unity) {
            $unity->reference = 'UNT-00' . strtoupper(Str::random(2));
        });
    }
}