<?php

namespace App\Models\Concerns;

use Illuminate\Support\Str;

trait UsesUuidPrimaryKey
{
    public static function bootUsesUuidPrimaryKey(): void
    {
        static::creating(function ($model) {
            if (config('ngbvd.uuid_primary_keys', false) && empty($model->{$model->getKeyName()})) {
                $model->{$model->getKeyName()} = (string) Str::uuid();
            }
        });
    }

    public function initializeUsesUuidPrimaryKey(): void
    {
        if (config('ngbvd.uuid_primary_keys', false)) {
            $this->incrementing = false;
            $this->keyType = 'string';
        }
    }

    public function getIncrementing(): bool
    {
        return config('ngbvd.uuid_primary_keys', false) ? false : parent::getIncrementing();
    }

    public function getKeyType(): string
    {
        return config('ngbvd.uuid_primary_keys', false) ? 'string' : parent::getKeyType();
    }
}
