<?php

namespace App\ScheduledTasks;

/**
 * Derives the system stock-sync task's schedule rules from the legacy
 * stock-sync-frequency setting. Shared by the seeder and the setting-change listener
 * so both stay in step.
 */
class StockSyncSchedule
{
    /**
     * @return array<int, array{frequency: string, time: string}>
     */
    public static function rulesForFrequency(?string $frequency): array
    {
        $times = match ($frequency) {
            'daily' => ['08:00'],
            'every-hour' => self::hourlyWindow(1),
            'every-two-hours' => self::hourlyWindow(2),
            default => ['09:00', '17:00'], // twice-daily
        };

        // Every day, matching today's cron (which has no weekday restriction)
        return array_map(fn (string $time) => ['frequency' => 'daily', 'time' => $time], $times);
    }

    /**
     * Run times across the 6am-10pm working window at the given hour step.
     *
     * @return array<int, string>
     */
    private static function hourlyWindow(int $step): array
    {
        $times = [];

        for ($hour = 6; $hour <= 22; $hour += $step) {
            $times[] = sprintf('%02d:00', $hour);
        }

        return $times;
    }
}
