<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity]
class IngestJob
{
    public const TYPE_DOCUMENT       = 'DOCUMENT';
    public const TYPE_GLOBAL_REINDEX = 'GLOBAL_REINDEX';

    /**
     * Special job type used when a DocumentVersion is activated.
     * Semantics: always re-ingest the selected version (even if it was previously INDEXED),
     * ensuring the index reflects the currently active version.
     */
    public const TYPE_DOCUMENT_VERSION_ACTIVATE = 'DOCUMENT_VERSION_ACTIVATE';

    /**
     * Job type used when a Document is hard-deleted from DB
     * and must be removed from index.ndjson + vector index.
     */
    public const TYPE_DOCUMENT_DELETE = 'DOCUMENT_DELETE';

    public const STATUS_QUEUED    = 'QUEUED';
    public const STATUS_RUNNING   = 'RUNNING';
    public const STATUS_COMPLETED = 'COMPLETED';
    public const STATUS_FAILED    = 'FAILED';
    public const STATUS_ABORTED   = 'ABORTED';

    #[ORM\Id]
    #[ORM\Column(type: 'uuid', unique: true)]
    private Uuid $id;

    #[ORM\Column(length: 30)]
    private string $type;

    #[ORM\Column(length: 20)]
    private string $status = self::STATUS_RUNNING;

    #[ORM\Column(type: 'uuid', nullable: true)]
    private ?Uuid $documentId = null;

    #[ORM\Column(type: 'uuid', nullable: true)]
    private ?Uuid $documentVersionId = null;

    #[ORM\Column]
    private \DateTimeImmutable $startedAt;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $finishedAt = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: true)]
    private ?User $startedBy = null;

    #[ORM\Column(nullable: true)]
    private ?string $logPath = null;

    #[ORM\Column(type: 'text', nullable: true)]
    private ?string $errorMessage = null;

    public function __construct(string $type, string $status = self::STATUS_RUNNING)
    {
        $this->id = Uuid::v4();
        $this->type = $type;
        $this->startedAt = new \DateTimeImmutable();
        $this->status = $status;
    }

    public function getId(): Uuid { return $this->id; }
    public function getType(): string { return $this->type; }
    public function getStatus(): string { return $this->status; }

    public function setDocumentId(?Uuid $id): void { $this->documentId = $id; }
    public function getDocumentId(): ?Uuid { return $this->documentId; }

    public function setDocumentVersionId(?Uuid $id): void { $this->documentVersionId = $id; }
    public function getDocumentVersionId(): ?Uuid { return $this->documentVersionId; }

    public function setStartedBy(?User $user): void { $this->startedBy = $user; }
    public function getStartedBy(): ?User { return $this->startedBy; }

    public function setLogPath(?string $path): void { $this->logPath = $path; }
    public function getLogPath(): ?string { return $this->logPath; }

    public function getStartedAt(): \DateTimeImmutable { return $this->startedAt; }
    public function getFinishedAt(): ?\DateTimeImmutable { return $this->finishedAt; }

    public function markCompleted(): void
    {
        $this->status = self::STATUS_COMPLETED;
        $this->finishedAt = new \DateTimeImmutable();
    }

    public function markFailed(string $message): void
    {
        $this->status = self::STATUS_FAILED;
        $this->errorMessage = $message;
        $this->finishedAt = new \DateTimeImmutable();
    }

    public function markAborted(): void
    {
        $this->status = self::STATUS_ABORTED;
        $this->finishedAt = new \DateTimeImmutable();
    }

    public function markRunning(): void
    {
        $this->status = self::STATUS_RUNNING;
    }

    public function getErrorMessage(): ?string
    {
        return $this->errorMessage;
    }
}
