<?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\Action\Ticket\Handler;

use Chill\TicketBundle\Action\Ticket\CreateTicketCommand;
use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\EmergencyStatusHistory;
use Chill\TicketBundle\Entity\StateEnum;
use Chill\TicketBundle\Entity\StateHistory;
use Chill\TicketBundle\Entity\Ticket;
use Symfony\Component\Clock\ClockInterface;

class CreateTicketCommandHandler
{
    public function __construct(private readonly ClockInterface $clock) {}

    public function __invoke(CreateTicketCommand $command): Ticket
    {
        $ticket = new Ticket();
        $ticket->setExternalRef($command->externalReference);

        // initialize the first states
        new StateHistory(StateEnum::OPEN, $ticket, $this->clock->now());
        new EmergencyStatusHistory(EmergencyStatusEnum::NO, $ticket, $this->clock->now());

        return $ticket;
    }
}
