<?php

use App\Jobs\RunBackupScheduleJob;
use App\Models\BackupSchedule;
use App\Models\BackupScheduleRun;
use App\Services\BackupScheduling\BackupScheduleDispatcher;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Bus;

it('dispatching the same slot twice creates only one run', function () {
    CarbonImmutable::setTestNow(CarbonImmutable::create(2026, 1, 5, 10, 0, 30, 'UTC'));

    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    BackupSchedule::query()->create([
        'tenant_id' => $tenant->id,
        'name' => 'Daily 10:00',
        'is_enabled' => true,
        'timezone' => 'UTC',
        'frequency' => 'daily',
        'time_of_day' => '10:00:00',
        'days_of_week' => null,
        'policy_types' => ['deviceConfiguration'],
        'include_foundations' => true,
        'retention_keep_last' => 30,
        'next_run_at' => null,
    ]);

    Bus::fake();

    $dispatcher = app(BackupScheduleDispatcher::class);

    $dispatcher->dispatchDue([$tenant->external_id]);
    $dispatcher->dispatchDue([$tenant->external_id]);

    expect(BackupScheduleRun::query()->count())->toBe(1);

    Bus::assertDispatchedTimes(RunBackupScheduleJob::class, 1);
});

it('treats a unique constraint collision as already-dispatched and advances next_run_at', function () {
    CarbonImmutable::setTestNow(CarbonImmutable::create(2026, 1, 5, 10, 0, 30, 'UTC'));

    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

    $schedule = BackupSchedule::query()->create([
        'tenant_id' => $tenant->id,
        'name' => 'Daily 10:00',
        'is_enabled' => true,
        'timezone' => 'UTC',
        'frequency' => 'daily',
        'time_of_day' => '10:00:00',
        'days_of_week' => null,
        'policy_types' => ['deviceConfiguration'],
        'include_foundations' => true,
        'retention_keep_last' => 30,
        'next_run_at' => null,
    ]);

    BackupScheduleRun::query()->create([
        'backup_schedule_id' => $schedule->id,
        'tenant_id' => $tenant->id,
        'scheduled_for' => CarbonImmutable::now('UTC')->startOfMinute()->toDateTimeString(),
        'status' => BackupScheduleRun::STATUS_RUNNING,
        'summary' => null,
    ]);

    Bus::fake();

    $dispatcher = app(BackupScheduleDispatcher::class);
    $dispatcher->dispatchDue([$tenant->external_id]);

    expect(BackupScheduleRun::query()->count())->toBe(1);
    Bus::assertNotDispatched(RunBackupScheduleJob::class);

    $schedule->refresh();
    expect($schedule->next_run_at)->not->toBeNull();
    expect($schedule->next_run_at->toDateTimeString())->toBe('2026-01-06 10:00:00');
});
