<?php

namespace App\Domain\Manga\Infrastructure\Scheduler;

use App\Domain\Manga\Application\Command\CheckMonitoredMangas;
use DateTimeImmutable;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;

#[AsSchedule]
class MonitoringSchedule implements ScheduleProviderInterface
{
    public function __construct(
        private CacheInterface $cache
    ) {
    }

    public function getSchedule(): Schedule
    {
        return (new Schedule())->add(
            // Toutes les 2 heures, vérifie les mangas qui n'ont pas été vérifiés depuis 2 heures
            RecurringMessage::every('2 hours', new CheckMonitoredMangas(
                new DateTimeImmutable('-2 hours')
            ))
        )->stateful($this->cache);
    }
}
