<?php

declare(strict_types=1);

use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentTriageReview;
use App\Support\BackupHealth\TenantBackupHealthAssessment;
use App\Support\PortfolioTriage\PortfolioArrivalContextToken;
use App\Support\Workspaces\WorkspaceOverviewBuilder;
use Carbon\CarbonImmutable;
use Tests\Feature\Concerns\BuildsPortfolioTriageFixtures;

uses(BuildsPortfolioTriageFixtures::class);

afterEach(function (): void {
    CarbonImmutable::setTestNow();
});

it('counts triage review progress only for the current visible affected set', function (): void {
    CarbonImmutable::setTestNow(CarbonImmutable::create(2026, 4, 10, 8, 0, 0, 'UTC'));

    [$user, $anchorTenant] = $this->makePortfolioTriageActor('Overview Anchor ManagedEnvironment');
    $anchorBackup = $this->seedPortfolioBackupConcern($anchorTenant, TenantBackupHealthAssessment::POSTURE_HEALTHY);
    workspaceOverviewSeedRestoreHistory($anchorTenant, $anchorBackup, 'completed');

    $reviewedTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Overview Reviewed ManagedEnvironment');
    $followUpTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Overview Follow-up ManagedEnvironment');
    $changedTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Overview Changed ManagedEnvironment');
    $calmTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Overview Calm ManagedEnvironment');

    foreach ([$reviewedTenant, $followUpTenant, $changedTenant] as $tenant) {
        $backupSet = $this->seedPortfolioBackupConcern($tenant, TenantBackupHealthAssessment::POSTURE_STALE);
        workspaceOverviewSeedRestoreHistory($tenant, $backupSet, 'completed');
    }

    $calmBackup = $this->seedPortfolioBackupConcern($calmTenant, TenantBackupHealthAssessment::POSTURE_HEALTHY);
    workspaceOverviewSeedRestoreHistory($calmTenant, $calmBackup, 'completed');

    $this->seedPortfolioTriageReview($reviewedTenant, PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH, ManagedEnvironmentTriageReview::STATE_REVIEWED, $user);
    $this->seedPortfolioTriageReview($followUpTenant, PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH, ManagedEnvironmentTriageReview::STATE_FOLLOW_UP_NEEDED, $user);
    $this->seedPortfolioTriageReview($changedTenant, PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH, ManagedEnvironmentTriageReview::STATE_REVIEWED, $user, changedFingerprint: true);

    $workspace = $anchorTenant->workspace()->firstOrFail();
    $overview = app(WorkspaceOverviewBuilder::class)->build($workspace, $user);

    $backupProgress = collect($overview['triage_review_progress'])
        ->firstWhere('concern_family', PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH);

    $backupMetric = collect($overview['summary_metrics'])->firstWhere('key', 'backup_attention_tenants');

    expect($backupProgress['affected_total'])->toBe(3)
        ->and($backupProgress['reviewed_count'])->toBe(1)
        ->and($backupProgress['follow_up_needed_count'])->toBe(1)
        ->and($backupProgress['changed_since_review_count'])->toBe(1)
        ->and($backupProgress['not_reviewed_count'])->toBe(0)
        ->and($backupMetric['description'])->toContain('Reviewed 1/3.')
        ->and($backupProgress['not_reviewed_destination']['url'])->toContain('review_state%5B0%5D=not_reviewed');
});

it('omits triage review progress when the visible workspace slice is calm', function (): void {
    [$user, $tenant] = $this->makePortfolioTriageActor('Calm Overview ManagedEnvironment');
    $backupSet = $this->seedPortfolioBackupConcern($tenant, TenantBackupHealthAssessment::POSTURE_HEALTHY);
    workspaceOverviewSeedRestoreHistory($tenant, $backupSet, 'completed');

    $workspace = $tenant->workspace()->firstOrFail();
    $overview = app(WorkspaceOverviewBuilder::class)->build($workspace, $user);

    expect($overview['triage_review_progress'])->toBe([]);
});
