<?php

namespace App\Models;

use App\Models\Concerns\BelongsToUser;
use App\Models\Vente;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Paiement extends Model
{
    use BelongsToUser, HasFactory;

    protected $guarded = [];

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

    public function vente()
    {
        return $this->belongsTo(Vente::class);
    }

    /**
     * Générer la référence du paiement lors de la création
     */
    protected static function booted()
    {
        static::creating(function ($paiement) {
            if (empty($paiement->reference)) {
                $paiement->reference = 'PAI-00' . strtoupper(Str::random(2));
            }
        });
    }
}
