<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Concerns;

use Builder\Core\Models\Revision;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;

/** @phpstan-require-extends Model */
trait HasRevisionHistory
{
    /** @return MorphMany<Revision, $this> */
    public function revisions(): MorphMany
    {
        return $this->morphMany(Revision::class, 'revisable')->latest('created_at');
    }

    public function latestRevision(): ?Revision
    {
        return $this->revisions()->first();
    }
}
