<?php

namespace Lucent\Revision;

use Lucent\Channel\ChannelService;
use Lucent\Edge\Edge;
use Lucent\Edge\EdgeCollection;
use Lucent\Primitive\Collection;
use Lucent\Record\Record;
use PhpOption\Option;

readonly class RevisionService
{

    public function __construct(
        private ChannelService $channelService,
        private RevisionRepo $revisionRepo,
    )
    {
    }

    /**
     * @return Collection<Revision>
     */
    public function getByRecordId(string $recordId): Collection
    {

        return $this->revisionRepo->getByRecordId($recordId);
    }

    /**
     * @return Option<Revision>
     */
    public function getByRecordIdAndVersion(string $recordId, int $version): Option
    {

        return $this->revisionRepo->getByRecordIdAndVersion($recordId, $version);
    }


    public function create(Record $record, EdgeCollection $edges): void
    {
        $schema = $this->channelService->getSchema($record->schema)->get();
        if($schema->revisions <= 0){
            return;
        }
        $revision = Revision::fromRecord($record, $edges);
        $this->revisionRepo->create($revision);
        $this->revisionRepo->cleanupRecord($record->id,$schema->revisions);
    }


}
