<?php

namespace App\Dto\Input;

use App\Entity\GitRepository;
use App\Repository\ImageRepositoryRepository;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use OpenApi\Annotations as OA;

/**
 * @OA\Schema(
 *     description="Datos de entrada para crear o actualizar un repositorio Git",
 *     required={"name", "repository"}
 * )
 */
final class GitRepositoryInput
{
    /**
     * @OA\Property(
     *     description="Nombre del repositorio Git",
     *     example="mi-repositorio"
     * )
     */
    #[Groups(['git-repository:write'])]
    #[Assert\NotBlank(message: 'El nombre del repositorio es requerido')]
    public ?string $name = null;

    /**
     * @OA\Property(
     *     description="Descripción del repositorio",
     *     example="Repositorio para el proyecto principal"
     * )
     */
    #[Groups(['git-repository:write'])]
    public ?string $description = null;

    /**
     * @OA\Property(
     *     description="ID del ImageRepository donde se creará el repositorio",
     *     example=1
     * )
     */
    #[Groups(['git-repository:write'])]
    #[Assert\NotNull(message: 'El ID del ImageRepository es requerido')]
    #[Assert\Type(type: 'integer', message: 'El ID del ImageRepository debe ser un número entero')]
    public ?int $repository = null; // Ahora es el ID del ImageRepository

    public function __construct(?GitRepository $gitRepository = null)
    {
        if (!$gitRepository) {
            return;
        }

        $this->name = $gitRepository->getName();
        $this->description = $gitRepository->getDescription();
        $this->repository = $gitRepository->getRepository()?->getId();
    }

    public function createOrUpdateEntity(?GitRepository $gitRepository = null): GitRepository
    {
        if (!$gitRepository) {
            $gitRepository = new GitRepository();
        }

        $gitRepository->setName($this->name);
        $gitRepository->setDescription($this->description);
        
        return $gitRepository;
    }
} 