<?php

declare(strict_types=1);

use App\Models\BackupItem;
use App\Models\BackupSet;
use App\Support\BackupQuality\BackupQualityResolver;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;

it('renders a clear backup-set summary when no degradations are detected', function (): void {
    $backupSet = new BackupSet([
        'status' => 'completed',
        'item_count' => 2,
    ]);

    $backupSet->setRelation('items', new EloquentCollection([
        new BackupItem([
            'payload' => ['id' => 'one'],
            'metadata' => [],
            'assignments' => [],
        ]),
        new BackupItem([
            'payload' => ['id' => 'two'],
            'metadata' => [],
            'assignments' => [],
        ]),
    ]));

    $summary = app(BackupQualityResolver::class)->summarizeBackupSet($backupSet);

    expect($summary->degradedItemCount)->toBe(0)
        ->and($summary->compactSummary)->toBe('No degradations detected across 2 items')
        ->and($summary->summaryMessage)->toBe('No degradations were detected across 2 captured items.');
});

it('aggregates degraded backup-set counts from existing item metadata', function (): void {
    $backupSet = new BackupSet([
        'status' => 'completed',
        'item_count' => 3,
    ]);

    $backupSet->setRelation('items', new EloquentCollection([
        new BackupItem([
            'payload' => ['id' => 'healthy'],
            'metadata' => [],
            'assignments' => [],
        ]),
        new BackupItem([
            'payload' => [],
            'metadata' => [
                'source' => 'metadata_only',
                'assignments_fetch_failed' => true,
            ],
            'assignments' => [],
        ]),
        new BackupItem([
            'payload' => ['id' => 'warning'],
            'metadata' => [
                'has_orphaned_assignments' => true,
                'integrity_warning' => 'Protected values are intentionally hidden.',
            ],
            'assignments' => [],
        ]),
    ]));

    $summary = app(BackupQualityResolver::class)->summarizeBackupSet($backupSet);

    expect($summary->degradedItemCount)->toBe(2)
        ->and($summary->metadataOnlyCount)->toBe(1)
        ->and($summary->assignmentIssueCount)->toBe(1)
        ->and($summary->orphanedAssignmentCount)->toBe(1)
        ->and($summary->integrityWarningCount)->toBe(1)
        ->and($summary->compactSummary)->toBe('2 degraded items • 1 metadata-only • 1 assignment issue • 1 orphaned assignment • 1 integrity warning')
        ->and($summary->nextAction)->toBe('Open the backup set detail and inspect degraded items before continuing into restore.');
});
