<?php

namespace App\Service;

use App\Entity\IngestJob;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Uid\Uuid;

final class IngestJobService
{
    public function __construct(private EntityManagerInterface $em)
    {
    }

    public function startJob(
        string  $type,
        ?User   $user = null,
        ?Uuid   $documentId = null,
        ?Uuid   $documentVersionId = null,
        ?string $logPath = null,
        string  $status = IngestJob::STATUS_RUNNING
    ): IngestJob
    {
        $job = new IngestJob($type, $status);
        $job->setStartedBy($user);
        $job->setDocumentId($documentId);
        $job->setDocumentVersionId($documentVersionId);
        $job->setLogPath($logPath);

        $this->em->persist($job);
        $this->em->flush();

        return $job;
    }

    public function markCompleted(IngestJob $job): void
    {
        $job->markCompleted();
        $this->em->flush();
    }

    public function markFailed(IngestJob $job, string $message): void
    {
        $job->markFailed($message);
        $this->em->flush();
    }

    public function markAborted(IngestJob $job): void
    {
        $job->markAborted();
        $this->em->flush();
    }
}
