<?php

it('registers an opening-time self-serve cleaner relay activation cron', function (): void {
    $tasks = require app_path('modules/selfserve/cron/tasks.php');

    expect($tasks)->toHaveCount(1);
    expect($tasks[0]['id'])->toBe('selfserve.activate_opening_cleaner_relays');
    expect($tasks[0]['legacy_name'])->toBe('SelfserveOpeningRelayActivationCron');
    expect($tasks[0]['handler'])->toBe('SelfserveOpeningRelayActivationCron');
    expect($tasks[0]['schedule'])->toBe(['type' => 'interval', 'seconds' => 60]);
});

it('activates only configured cleaner relays after department opening time', function (): void {
    $cronContent = file_get_contents(app_path('cron/Cron.php'));

    expect($cronContent)->not->toBeFalse();
    expect($cronContent)->toContain('function SelfserveOpeningRelayActivationCron(): array');
    expect($cronContent)->toContain("new DateTimeZone('Europe/Copenhagen')");
    expect($cronContent)->toContain('department_time_bookings_opening_hours');
    expect($cronContent)->toContain("dv.variable = 'selfserve_enabled'");
    expect($cronContent)->toContain('dl.selfserve_enabled');
    expect($cronContent)->toContain('relay_machine_cleaner_id');
    expect($cronContent)->toContain('$nowSeconds >= $opensAtSeconds && $nowSeconds < $closesAtSeconds');
    expect($cronContent)->toContain('setMachineCleanerRelayStatusHard(true)');
    expect($cronContent)->toContain('selfserve:opening-cleaner-relays:');
    expect($cronContent)->not->toContain('setMachineRelayStatusHard(true)');
    expect($cronContent)->not->toContain('setMachineProgramPickerRelayStatusHard(true)');
});
