<?php

use App\Filament\Resources\RestoreRunResource\Pages\ListRestoreRuns;
use App\Models\BackupSet;
use App\Models\RestoreRun;
use App\Models\ManagedEnvironment;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

test('restore run archive action does not archive non-deletable runs', function () {
    $tenant = ManagedEnvironment::factory()->create();

    $backupSet = BackupSet::create([
        'managed_environment_id' => $tenant->id,
        'name' => 'Set RR',
        'status' => 'completed',
        'item_count' => 0,
    ]);

    $running = RestoreRun::create([
        'managed_environment_id' => $tenant->id,
        'backup_set_id' => $backupSet->id,
        'status' => 'running',
        'is_dry_run' => true,
    ]);

    [$user, $tenant] = createUserWithTenant(tenant: $tenant, role: 'owner');
    setAdminPanelContext($tenant);

    Livewire::actingAs($user)
        ->test(ListRestoreRuns::class)
        ->callTableAction('archive', $running);

    expect(RestoreRun::withTrashed()->find($running->id)?->trashed())->toBeFalse();
});
