<?php

namespace App\Controller;

use App\Entity\Trace;
use App\Model\TraceStatus;
use App\Repository\TraceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;

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

    public function __invoke(string $uuid): JsonResponse
    {
        $trace = $this->traceRepository->findOneBy(['uuid' => $uuid]);
        
        if (!$trace) {
            return new JsonResponse(data: 'Trace not found', status: Response::HTTP_NOT_FOUND);
        }

        $trace->setStatus(TraceStatus::SUCCESS);
        $trace->setOutput('Trace marked as success manually');
        $trace->setFinishedAt(new \DateTime());
        
        $this->entityManager->persist($trace);
        $this->entityManager->flush();

        return new JsonResponse(data: 'Trace marked as success successfully', status: Response::HTTP_OK);
    }
} 