<?php

namespace App\Scheduler;

use App\Message\IngestionScheduleMessage;
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;

/**
 * Planifie l'ingestion satellite toutes les 6h.
 * Le Schedule est mis en cache pour éviter les recréations inutiles.
 */
#[AsSchedule('main')]
final class MainSchedule implements ScheduleProviderInterface
{
    private ?Schedule $schedule = null;

    public function __construct(private CacheInterface $cache) {}

    public function getSchedule(): Schedule
    {
        return $this->schedule ??= (new Schedule())
            ->add(
                // 06:00, 12:00, 18:00, 00:00 UTC — données satellite disponibles ~2h après le passage
                RecurringMessage::cron('0 0,6,12,18 * * *', new IngestionScheduleMessage())
            )
            ->stateful($this->cache);
    }
}
