<?php

declare(strict_types=1);

namespace ImovelZapAi\Shared\Infrastructure\Persistence\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

/** @phpstan-require-extends Model */
trait HasUuidPrimaryKey
{
    public function initializeHasUuidPrimaryKey(): void
    {
        $this->keyType = 'string';
        $this->incrementing = false;
    }

    protected static function bootHasUuidPrimaryKey(): void
    {
        static::creating(static function (Model $model): void {
            $key = $model->getKeyName();

            if ($model->getAttribute($key) === null) {
                $model->setAttribute($key, (string) Str::uuid());
            }
        });
    }
}
