<?php

declare(strict_types=1);

use App\Jobs\Supplier\RefreshSupplierSessionJob;
use App\Jobs\Supplier\SyncSupplierProjectsJob;
use Illuminate\Console\Scheduling\Schedule;

/*
 * Крон supplier-sync переехал 20:30 → 18:00 МСК (Task 9, spec §4.7) —
 * запас ~3 часа до портального дедлайна 21:00 на эскалацию ярус 2/3.
 * Session refresh — на 15 мин раньше sync (17:45).
 */

it('SyncSupplierProjectsJob is scheduled at 18:05 MSK (after billing:preflight-sweep @18:00)', function (): void {
    // Billing v2 Spec C §3.8: sync сдвинут с 18:00 на 18:05, чтобы
    // billing:preflight-sweep (18:00) успел проставить frozen-флаги.
    $schedule = app(Schedule::class);
    $events = collect($schedule->events());

    $sync = $events->first(fn ($e) => str_contains((string) $e->description, SyncSupplierProjectsJob::class)
        || str_contains((string) $e->command, 'SyncSupplierProjectsJob'));

    expect($sync)->not->toBeNull();
    expect($sync->expression)->toBe('5 18 * * *');
    expect($sync->timezone)->toBe('Europe/Moscow');
});

it('Daily RefreshSupplierSessionJob is scheduled at 17:45 MSK', function (): void {
    $schedule = app(Schedule::class);
    $events = collect($schedule->events());

    $daily = $events->first(fn ($e) => (str_contains((string) $e->description, RefreshSupplierSessionJob::class)
        || str_contains((string) $e->command, 'RefreshSupplierSessionJob'))
        && $e->expression === '45 17 * * *');

    expect($daily)->not->toBeNull();
    expect($daily->timezone)->toBe('Europe/Moscow');
});
