<?php

declare(strict_types=1);

namespace App\Module\TimeTracking\Infrastructure\ApiPlatform\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Module\TimeTracking\Domain\Entity\TimeEntry;
use App\Module\TimeTracking\Domain\Repository\TimeEntryRepositoryInterface;
use Symfony\Bundle\SecurityBundle\Security;

/**
 * @implements ProviderInterface<TimeEntry>
 */
final readonly class ActiveTimeEntryProvider implements ProviderInterface
{
    public function __construct(
        private Security $security,
        private TimeEntryRepositoryInterface $timeEntryRepository,
    ) {}

    public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
    {
        $user = $this->security->getUser();

        if (!$user) {
            return [];
        }

        $entry = $this->timeEntryRepository->findActiveByUser($user);

        return $entry ? [$entry] : [];
    }
}
