<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\HardwareTypeOutput;
use App\Entity\Hardware;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

final class HardwareInput
{
    #[Assert\NotBlank(message: 'validators.hardware.name.not_blank')]
    #[Groups(['hardware:write'])]
    #[ApiProperty(description: 'The name of the hardware', example: "Hardware 1")]
    public ?string $name = null;

    #[Groups(['hardware:write'])]
    #[ApiProperty(description: 'The description of the hardware', example: "Hardware 1 description")]
    public ?string $description = null;

    #[Assert\NotNull(message: 'validators.hardware.type.not_null')]
    #[Groups(['hardware:write'])]
    #[ApiProperty(description: 'The type of the hardware', example: "Server")]
    public ?HardwareTypeOutput $type = null;

    public function __construct(?Hardware $hardware = null)
    {
        if (!$hardware) {
            return;
        }

        $this->name = $hardware->getName();
        $this->description = $hardware->getDescription();
        $this->type = new HardwareTypeOutput($hardware->getType());
    }

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

        $hardware->setName($this->name);
        $hardware->setDescription($this->description);
        $hardware->setType($this->type->getEntity());

        return $hardware;
    }
}