<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait GeneratesReference
{
    public static function bootGeneratesReference()
    {
        static::created(function ($model) {

            // Use a prefix property from the model
            if (!property_exists($model, 'reference_prefix')) {
                return;
            }

            $prefix = $model->reference_prefix;

            $yearMonth = now()->format('ym');

            // Combine ID + day
            $combined = $model->id . now()->format('d');

            $hex = strtoupper(dechex($combined));

            $model->reference = "{$prefix}-{$yearMonth}-{$hex}";

            $model->saveQuietly();
        });
    }
}
