<?php

namespace App\Models;

use App\Models\Concerns\BelongsToUser;
use Deligoez\LaravelModelHashId\Traits\HasHashId;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Depense extends Model
{
    use BelongsToUser, HasFactory, SoftDeletes, HasHashId;

    protected $fillable = ['reference', 'libelle', 'magasin_id', 'montant', 'date_depense', 'user_id'];

    public function magasin(): BelongsTo
    {
        return $this->belongsTo(Magasin::class);
    }

    protected $casts = [
        'date_depense' => 'date',
        'montant' => 'decimal:0',
    ];

    protected static function booted()
    {
        static::creating(function ($depense) {
            if (empty($depense->reference)) {
                $depense->reference = 'DEP-00' . strtoupper(Str::random(2));
            }
        });
    }
}