<?php

use App\Jobs\Operations\BackupSetRestoreWorkerJob;
use App\Models\BackupItem;
use App\Models\BackupSet;
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Models\User;
use App\Services\OperationRunService;
use App\Services\Operations\TargetScopeConcurrencyLimiter;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);
test('bulk backup set restore job restores archived sets and their items', function () {
    $tenant = Tenant::factory()->create(['is_current' => true]);
    $user = User::factory()->create();

    $set = BackupSet::create([
        'tenant_id' => $tenant->id,
        'name' => 'Backup',
        'status' => 'completed',
        'item_count' => 1,
    ]);

    $item = BackupItem::create([
        'tenant_id' => $tenant->id,
        'backup_set_id' => $set->id,
        'policy_id' => null,
        'policy_identifier' => 'policy-1',
        'policy_type' => 'deviceConfiguration',
        'platform' => 'windows10',
        'payload' => ['id' => 'policy-1'],
        'metadata' => null,
    ]);

    $set->delete();

    $set->refresh();
    expect($set->trashed())->toBeTrue();
    expect(BackupItem::withTrashed()->find($item->id)?->trashed())->toBeTrue();

    $run = OperationRun::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'user_id' => $user->getKey(),
        'initiator_name' => $user->name,
        'type' => 'backup_set.restore',
        'status' => 'running',
        'outcome' => 'pending',
        'context' => ['target_scope' => ['entra_tenant_id' => 'entra-test-tenant']],
        'summary_counts' => ['total' => 1, 'processed' => 0],
        'failure_summary' => [],
    ]);

    (new BackupSetRestoreWorkerJob(
        tenantId: (int) $tenant->getKey(),
        userId: (int) $user->getKey(),
        backupSetId: (int) $set->getKey(),
        operationRun: $run,
    ))->handle(app(OperationRunService::class), app(TargetScopeConcurrencyLimiter::class));

    $set->refresh();
    expect($set->trashed())->toBeFalse();

    $item->refresh();
    expect($item->trashed())->toBeFalse();

    $run->refresh();
    expect($run->status)->toBe('completed');
    expect((int) ($run->summary_counts['processed'] ?? 0))->toBe(1);
    expect((int) ($run->summary_counts['succeeded'] ?? 0))->toBe(1);
    expect((int) ($run->summary_counts['skipped'] ?? 0))->toBe(0);
    expect((int) ($run->summary_counts['failed'] ?? 0))->toBe(0);
});

test('bulk backup set restore job skips active sets', function () {
    $tenant = Tenant::factory()->create(['is_current' => true]);
    $user = User::factory()->create();

    $set = BackupSet::create([
        'tenant_id' => $tenant->id,
        'name' => 'Backup',
        'status' => 'completed',
        'item_count' => 0,
    ]);

    $run = OperationRun::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'user_id' => $user->getKey(),
        'initiator_name' => $user->name,
        'type' => 'backup_set.restore',
        'status' => 'running',
        'outcome' => 'pending',
        'context' => ['target_scope' => ['entra_tenant_id' => 'entra-test-tenant']],
        'summary_counts' => ['total' => 1, 'processed' => 0],
        'failure_summary' => [],
    ]);

    (new BackupSetRestoreWorkerJob(
        tenantId: (int) $tenant->getKey(),
        userId: (int) $user->getKey(),
        backupSetId: (int) $set->getKey(),
        operationRun: $run,
    ))->handle(app(OperationRunService::class), app(TargetScopeConcurrencyLimiter::class));

    $set->refresh();
    expect($set->trashed())->toBeFalse();

    $run->refresh();
    expect($run->status)->toBe('completed');
    expect((int) ($run->summary_counts['processed'] ?? 0))->toBe(1);
    expect((int) ($run->summary_counts['succeeded'] ?? 0))->toBe(0);
    expect((int) ($run->summary_counts['skipped'] ?? 0))->toBe(1);
    expect((int) ($run->summary_counts['failed'] ?? 0))->toBe(0);
});
