<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Seed\Model;

use CpCreation\VitiCore\Behavior\Impl\Timestamp;
use CpCreation\VitiCore\Seed\Repository\SeedStateRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Bridge\Doctrine\Types\UuidType;

#[ORM\Entity(repositoryClass: SeedStateRepository::class)]
#[ORM\Table(name: 'seed_state')]
#[ORM\HasLifecycleCallbacks]
class SeedState
{
    use Timestamp;

    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, unique: true)]
    public UuidInterface $id;

    public function getId(): UuidInterface
    {
        return $this->id;
    }

    #[ORM\Column(type: Types::STRING, length: 80)]
    public private(set) string $seedName;

    #[ORM\Column(type: Types::INTEGER, options: ['default' => 0])]
    public private(set) int $version = 0;

    #[ORM\Column(type: UuidType::NAME, unique: true)]
    public private(set) UuidInterface $runId;

    public function __construct()
    {
        $this->id = Uuid::uuid4();
    }

    public static function create(
        string $seedName,
        string $runId,
        int $version = 0,
    ): self {
        $self = new self();
        $self->runId = Uuid::fromString($runId);
        $self->seedName = $seedName;
        $self->version = $version;

        return $self;
    }

    public function moveTo(int $newVersion, string $runId): void
    {
        $this->version = $newVersion;
        $this->runId = Uuid::fromString($runId);
    }
}
