<?php

namespace App\Controller;

use App\Dto\Input\CancelMultipleTracesInput;
use App\Entity\Trace;
use App\Model\TraceStatus;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class CancelMultipleTracesAction extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    )
    {
    }

    public function __invoke(CancelMultipleTracesInput $input): JsonResponse
    {
        foreach ($input->traces as $trace) {
            /** @var Trace $trace */
            $trace = $trace->getEntity();

            $trace->setStatus(TraceStatus::CANCELLED);
            $this->entityManager->persist($trace);
        }

        $this->entityManager->flush();

        return new JsonResponse(data: 'Traces cancelled successfully', status: Response::HTTP_OK);
    }
}