<?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\MainBundle\Tests\Services\UserGroup;

use Chill\MainBundle\Entity\CronJobExecution;
use Chill\MainBundle\Service\UserGroup\UserGroupRelatedToUserJobSyncCronJob;
use Chill\MainBundle\Service\UserGroup\UserGroupRelatedToUserJobSyncInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;

/**
 * @internal
 *
 * @coversNothing
 */
class UserGroupRelatedToUserJobSyncCronJobTest extends TestCase
{
    use ProphecyTrait;

    /**
     * @dataProvider canRunDataProvider
     */
    public function testCanRun(\DateTimeImmutable $now, ?\DateTimeImmutable $lastStartExecution, bool $exected): void
    {
        $clock = new MockClock($now);
        $job = $this->prophesize(UserGroupRelatedToUserJobSyncInterface::class);

        $cronJob = new UserGroupRelatedToUserJobSyncCronJob($clock, $job->reveal());

        if (null !== $lastStartExecution) {
            $lastExecution = new CronJobExecution('user-group-related-to-user-job-sync');
            $lastExecution->setLastStart($lastStartExecution);
        }

        $actual = $cronJob->canRun($lastExecution ?? null);

        self::assertEquals($exected, $actual);
    }

    public static function canRunDataProvider(): iterable
    {
        $now = new \DateTimeImmutable('2025-04-27T00:00:00Z');

        yield 'never executed' => [$now, null, true];
        yield 'executed 12 hours ago' => [$now, new \DateTimeImmutable('2025-04-26T12:00:00Z'), false];
        yield 'executed more than 12 hours ago' => [$now, new \DateTimeImmutable('2025-04-25T12:00:00Z'), true];
    }
}
