<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle;

use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\Motive;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;

class MotiveDTO
{
    public function __construct(
        #[Assert\NotBlank()]
        public array $label = [],
        public bool $active = true,
        public Collection $supplementaryComments = new ArrayCollection(),
        public ?EmergencyStatusEnum $makeTicketEmergency = null,
        public float $ordering = 0.0,
    ) {
        if ($this->supplementaryComments->isEmpty()) {
            $this->supplementaryComments = new ArrayCollection();
        }
    }

    public static function fromMotive(Motive $motive): self
    {
        $supplementaryCommentsCollection = new ArrayCollection();

        foreach ($motive->getSupplementaryComments() as $comment) {
            $supplementaryCommentsCollection->add($comment['label']);
        }

        return new self(
            label: $motive->getLabel(),
            active: $motive->isActive(),
            supplementaryComments: $supplementaryCommentsCollection,
            makeTicketEmergency: $motive->getMakeTicketEmergency(),
            ordering: $motive->getOrdering(),
        );
    }

    public function applyToMotive(Motive $motive): void
    {
        $motive->setLabel($this->label);
        $motive->setActive($this->active);
        $motive->setMakeTicketEmergency($this->makeTicketEmergency);
        $motive->setOrdering($this->ordering);

        $supplementaryCommentsArray = [];

        foreach ($this->supplementaryComments as $supplementaryComment) {
            $supplementaryCommentsArray[] = ['label' => $supplementaryComment];
        }

        $motive->setSupplementaryComment($supplementaryCommentsArray);
    }
}
