<?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\Service\UserGroup;

use Chill\MainBundle\Cron\CronJobInterface;
use Chill\MainBundle\Entity\CronJobExecution;
use Symfony\Component\Clock\ClockInterface;

final readonly class UserGroupRelatedToUserJobSyncCronJob implements CronJobInterface
{
    private const KEY = 'user-group-related-to-user-job-sync';

    public function __construct(private ClockInterface $clock, private UserGroupRelatedToUserJobSyncInterface $userJobSync) {}

    public function canRun(?CronJobExecution $cronJobExecution): bool
    {
        if (null === $cronJobExecution) {
            return true;
        }

        return $cronJobExecution->getLastStart() < $this->clock->now()->sub(new \DateInterval('P1D'));
    }

    public function getKey(): string
    {
        return self::KEY;
    }

    public function run(array $lastExecutionData): ?array
    {
        return ($this->userJobSync)();
    }
}
