<?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 Stock extends Model
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable, HasHashId, HasRoles, SoftDeletes;

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

    /**
     * Relations
     */

    // Produit du stock
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    // Utilisateur qui a créé le stock
    public function user()
    {
        return $this->belongsTo(User::class);
    }

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