<?php

namespace App\Service\Trace;

use App\Entity\Client;
use App\Entity\Command;
use App\Entity\Trace;
use Doctrine\ORM\EntityManagerInterface;

readonly class CreateService
{
    public function __construct(
        private EntityManagerInterface $entityManager
    )
    {
    }

    public function __invoke(?Client $client = null, ?string $command, string $status, ?string $jobId = '', ?array $input = [], ?string $output = null): Trace
    {
        $trace = new Trace();
        $trace->setClient($client);
        $trace->setCommand($command);
        $trace->setStatus($status);
        $trace->setJobId($jobId);
        $trace->setExecutedAt(new \DateTime());
        $trace->setInput($input);
        $trace->setOutput($output);

        $this->entityManager->persist($trace);
        $this->entityManager->flush();

        return $trace;
    }
}