<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Dto\Output\OperativeSystemOutput;
use App\Dto\Output\OrganizationalUnitOutput;
use App\Entity\SoftwareProfile;
use Symfony\Component\Serializer\Annotation\Groups;

final class SoftwareProfileInput
{
    #[Groups(['software-profile:write'])]
    #[ApiProperty(description: 'The description of the software profile', example: "Software 1 description")]
    public ?string $description = null;

    #[Groups(['software-profile:write'])]
    #[ApiProperty(description: 'Comments of the software profile', example: "Software 1")]
    public ?string $comments = null;

    #[Groups(['software-profile:write'])]
    #[ApiProperty(description: 'The organizational unit of the software profile')]
    public ?OrganizationalUnitOutput $organizationalUnit = null;

    #[Groups(['software-profile:write'])]
    #[ApiProperty(description: 'The operative system of the software profile')]
    public ?OperativeSystemOutput $operativeSystem = null;


    public function __construct(?SoftwareProfile $softwareProfile = null)
    {
        if (!$softwareProfile) {
            return;
        }

        $this->description = $softwareProfile->getDescription();
        $this->comments = $softwareProfile->getComments();
        if($softwareProfile->getOrganizationalUnit()) {
            $this->organizationalUnit = new OrganizationalUnitOutput($softwareProfile->getOrganizationalUnit());
        }

        if($softwareProfile->getOperativeSystem()) {
            $this->operativeSystem = new OperativeSystemOutput($softwareProfile->getOperativeSystem());
        }
    }

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

        $softwareProfile->setDescription($this->description);
        $softwareProfile->setComments($this->comments);
        if ($this->organizationalUnit) {
            $softwareProfile->setOrganizationalUnit($this->organizationalUnit->getEntity());
        }

        if ($this->operativeSystem) {
            $softwareProfile->setOperativeSystem($this->operativeSystem->getEntity());
        }

        return $softwareProfile;
    }
}