<?php

namespace App\Dto\Output;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Get;
use App\Entity\GitRepository;
use Symfony\Component\Serializer\Annotation\Groups;
use OpenApi\Annotations as OA;

/**
 * @OA\Schema(
 *     description="Datos de salida de un repositorio Git"
 * )
 */
#[Get(shortName: 'GitRepository')]
final class GitRepositoryOutput
{
    /**
     * @OA\Property(
     *     description="UUID del repositorio Git",
     *     example="123e4567-e89b-12d3-a456-426614174000"
     * )
     */
    #[Groups(['git-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'El UUID del repositorio Git',
        example: '123e4567-e89b-12d3-a456-426614174000'
    )]
    public string $uuid = '';

    /**
     * @OA\Property(
     *     description="ID del repositorio Git",
     *     example=1
     * )
     */
    #[Groups(['git-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'El ID del repositorio Git',
        example: 1
    )]
    public int $id = 0;

    /**
     * @OA\Property(
     *     description="Nombre del repositorio Git",
     *     example="mi-repositorio"
     * )
     */
    #[Groups(['git-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'El nombre del repositorio Git',
        example: 'mi-repositorio'
    )]
    public string $name = '';

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

    /**
     * @OA\Property(
     *     description="Servidor donde se encuentra el repositorio",
     *     example={"id": 1, "name": "Servidor Principal", "ip": "192.168.1.100"}
     * )
     */
    #[Groups(['git-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'El servidor donde se encuentra el repositorio',
        example: null
    )]
    public ?ImageRepositoryOutput $repository = null;

    /**
     * @OA\Property(
     *     description="Fecha de creación del repositorio",
     *     example="2024-01-01T00:00:00+00:00"
     * )
     */
    #[Groups(['git-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'Fecha de creación del repositorio',
        example: '2024-01-01T00:00:00+00:00'
    )]
    public ?\DateTime $createdAt = null;

    /**
     * @OA\Property(
     *     description="Usuario que creó el repositorio",
     *     example="admin"
     * )
     */
    #[Groups(['git-repository:read', 'git-image-repository:read'])]
    #[ApiProperty(
        description: 'Usuario que creó este registro',
        example: 'admin'
    )]
    public ?string $createdBy = null;

    public function __construct(GitRepository $gitRepository, array $context = [])
    {
        if ($gitRepository->getId()) {
            $this->uuid = $gitRepository->getUuid()->toString();
            $this->id = $gitRepository->getId();
        } else {
            $this->uuid = $gitRepository->getUuid()->toString();
            $this->id = 0;
        }

        $this->name = $gitRepository->getName();
        $this->description = $gitRepository->getDescription();
        
        if ($gitRepository->getRepository()) {
            $this->repository = new ImageRepositoryOutput($gitRepository->getRepository());
        }
        
        $this->createdAt = $gitRepository->getCreatedAt() ?: new \DateTime();
        $this->createdBy = $gitRepository->getCreatedBy();
    }
} 