<?php

declare(strict_types=1);

use App\Filament\Resources\ManagedEnvironmentResource\Pages\ListManagedEnvironments;
use App\Filament\Resources\ManagedEnvironmentResource\Pages\ViewManagedEnvironment;
use App\Models\AuditLog;
use App\Services\Auth\CapabilityResolver;
use App\Support\Auth\Capabilities;
use App\Models\ManagedEnvironment;
use App\Models\ManagedEnvironmentTriageReview;
use App\Support\Audit\AuditActionId;
use App\Support\BackupHealth\TenantBackupHealthAssessment;
use App\Support\PortfolioTriage\PortfolioArrivalContextToken;
use App\Support\Tenants\TenantRecoveryTriagePresentation;
use Carbon\CarbonImmutable;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Livewire\Livewire;
use App\Support\Workspaces\WorkspaceContext;
use Tests\Feature\Concerns\BuildsPortfolioTriageFixtures;

uses(BuildsPortfolioTriageFixtures::class);

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

it('renders review-state badges and all four review-state filters for the current backup slice', function (): void {
    CarbonImmutable::setTestNow(CarbonImmutable::create(2026, 4, 10, 8, 0, 0, 'UTC'));

    [$user, $anchorTenant] = $this->makePortfolioTriageActor('Anchor Backup ManagedEnvironment');

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

    foreach ([$reviewedTenant, $followUpTenant, $changedTenant, $notReviewedTenant] as $tenant) {
        $this->seedPortfolioBackupConcern($tenant, TenantBackupHealthAssessment::POSTURE_STALE);
    }

    $this->seedPortfolioBackupConcern($calmTenant, TenantBackupHealthAssessment::POSTURE_HEALTHY);

    $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);

    $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'backup_posture' => [TenantBackupHealthAssessment::POSTURE_STALE],
    ])
        ->assertTableColumnExists('review_state')
        ->assertTableColumnFormattedStateSet('review_state', 'Reviewed', $reviewedTenant)
        ->assertTableColumnFormattedStateSet('review_state', 'Follow-up needed', $followUpTenant)
        ->assertTableColumnFormattedStateSet('review_state', 'Changed since review', $changedTenant)
        ->assertTableColumnFormattedStateSet('review_state', 'Not reviewed', $notReviewedTenant)
        ->assertDontSee('Calm Backup ManagedEnvironment');

    $reviewedNames = $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'backup_posture' => [TenantBackupHealthAssessment::POSTURE_STALE],
    ])
        ->filterTable('review_state', [ManagedEnvironmentTriageReview::STATE_REVIEWED])
        ->instance()
        ->getFilteredTableQuery()
        ?->pluck('managed_environments.name')
        ->all();

    $followUpNames = $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'backup_posture' => [TenantBackupHealthAssessment::POSTURE_STALE],
    ])
        ->filterTable('review_state', [ManagedEnvironmentTriageReview::STATE_FOLLOW_UP_NEEDED])
        ->instance()
        ->getFilteredTableQuery()
        ?->pluck('managed_environments.name')
        ->all();

    $changedNames = $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'backup_posture' => [TenantBackupHealthAssessment::POSTURE_STALE],
    ])
        ->filterTable('review_state', [ManagedEnvironmentTriageReview::DERIVED_STATE_CHANGED_SINCE_REVIEW])
        ->instance()
        ->getFilteredTableQuery()
        ?->pluck('managed_environments.name')
        ->all();

    $notReviewedNames = $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'backup_posture' => [TenantBackupHealthAssessment::POSTURE_STALE],
    ])
        ->filterTable('review_state', [ManagedEnvironmentTriageReview::DERIVED_STATE_NOT_REVIEWED])
        ->instance()
        ->getFilteredTableQuery()
        ?->pluck('managed_environments.name')
        ->all();

    expect($reviewedNames)->toBe(['Reviewed Backup ManagedEnvironment'])
        ->and($followUpNames)->toBe(['Follow-up Backup ManagedEnvironment'])
        ->and($changedNames)->toBe(['Changed Backup ManagedEnvironment'])
        ->and($notReviewedNames)->toBe(['Not Reviewed Backup ManagedEnvironment']);
});

it('uses the highest-priority current concern family when the registry slice is mixed', function (): void {
    [$user, $anchorTenant] = $this->makePortfolioTriageActor('Anchor Mixed ManagedEnvironment');
    $mixedTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Mixed Concern ManagedEnvironment');

    $backupSet = $this->seedPortfolioBackupConcern($mixedTenant, TenantBackupHealthAssessment::POSTURE_STALE);
    $this->seedPortfolioRecoveryConcern($mixedTenant, 'failed', $backupSet);
    $this->seedPortfolioTriageReview(
        $mixedTenant,
        PortfolioArrivalContextToken::FAMILY_RECOVERY_EVIDENCE,
        ManagedEnvironmentTriageReview::STATE_FOLLOW_UP_NEEDED,
        $user,
    );

    $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'triage_sort' => TenantRecoveryTriagePresentation::TRIAGE_SORT_WORST_FIRST,
    ])
        ->assertTableColumnFormattedStateSet('review_state', 'Follow-up needed', $mixedTenant)
        ->assertSee('Recovery evidence');
});

it('keeps review-state mutations in overflow with a preview-confirmed write path', function (): void {
    [$user, $anchorTenant] = $this->makePortfolioTriageActor('Anchor Action ManagedEnvironment', workspaceRole: 'manager');
    $actionTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Action Backup ManagedEnvironment');
    createMinimalUserWithTenant(tenant: $actionTenant, user: $user, role: 'owner', workspaceRole: 'manager');
    $this->seedPortfolioBackupConcern($actionTenant, TenantBackupHealthAssessment::POSTURE_STALE);

    $component = $this->portfolioTriageRegistryList($user, $anchorTenant, [
        'backup_posture' => [TenantBackupHealthAssessment::POSTURE_STALE],
    ])
        ->assertTableActionVisible('openTenant', $actionTenant)
        ->assertTableActionEnabled('markReviewed', $actionTenant)
        ->assertTableActionExists('markReviewed', fn (Action $action): bool => $action->isConfirmationRequired()
            && str_contains((string) $action->getModalDescription(), 'Concern family: Backup health')
            && str_contains((string) $action->getModalDescription(), 'Target state: Reviewed')
            && str_contains((string) $action->getModalDescription(), 'TenantPilot only'), $actionTenant)
        ->assertTableActionExists('markFollowUpNeeded', fn (Action $action): bool => $action->isConfirmationRequired(), $actionTenant);

    $action = $component->instance()->getAction([
        [
            'name' => 'markReviewed',
            'context' => [
                'table' => true,
                'recordKey' => (string) $actionTenant->getKey(),
            ],
        ],
    ]);

    expect(app(CapabilityResolver::class)->can($user, $actionTenant, Capabilities::MANAGED_ENVIRONMENT_TRIAGE_REVIEW_MANAGE))->toBeTrue();

    expect($action)->not->toBeNull()
        ->and($action?->getRecord())->toBeInstanceOf(ManagedEnvironment::class)
        ->and((int) $action?->getRecord()?->getKey())->toBe((int) $actionTenant->getKey())
        ->and($action?->isDisabled())->toBeFalse()
        ->and($component->instance()->mountedActionShouldOpenModal($action))->toBeTrue();

    $component->mountAction([
        [
            'name' => 'markReviewed',
            'context' => [
                'table' => true,
                'recordKey' => (string) $actionTenant->getKey(),
            ],
        ],
    ]);

    $mountedAction = $component->instance()->getMountedAction();

    expect($mountedAction)->not->toBeNull()
        ->and($mountedAction?->getRecord())->toBeInstanceOf(ManagedEnvironment::class)
        ->and((int) $mountedAction?->getRecord()?->getKey())->toBe((int) $actionTenant->getKey());

    $component
        ->callMountedAction();

    expect(ManagedEnvironmentTriageReview::query()
            ->where('managed_environment_id', (int) $actionTenant->getKey())
            ->where('concern_family', PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH)
            ->where('current_state', ManagedEnvironmentTriageReview::STATE_REVIEWED)
            ->whereNull('resolved_at')
            ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $actionTenant->workspace_id)
            ->where('managed_environment_id', (int) $actionTenant->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentTriageReviewMarkedReviewed->value)
            ->exists())->toBeTrue()
        ->and($component->instance())->toBeInstanceOf(ListManagedEnvironments::class);
});

it('keeps review-state mutations available on the tenant detail header for the current concern', function (): void {
    [$user, $anchorTenant] = $this->makePortfolioTriageActor('Anchor Detail Action ManagedEnvironment', workspaceRole: 'manager');
    $actionTenant = $this->makePortfolioTriagePeer($user, $anchorTenant, 'Detail Action Backup ManagedEnvironment');
    createMinimalUserWithTenant(tenant: $actionTenant, user: $user, role: 'owner', workspaceRole: 'manager');
    $this->seedPortfolioBackupConcern($actionTenant, TenantBackupHealthAssessment::POSTURE_STALE);

    $this->actingAs($user);
    setAdminPanelContext();
    session([WorkspaceContext::SESSION_KEY => (int) $actionTenant->workspace_id]);

    $component = Livewire::actingAs($user)
        ->test(ViewManagedEnvironment::class, ['record' => $actionTenant->getRouteKey()])
        ->assertActionVisible('markReviewed')
        ->assertActionEnabled('markReviewed')
        ->assertActionExists('markReviewed', fn (Action $action): bool => $action->isConfirmationRequired()
            && str_contains((string) $action->getModalDescription(), 'Concern family: Backup health')
            && str_contains((string) $action->getModalDescription(), 'Target state: Reviewed')
            && str_contains((string) $action->getModalDescription(), 'TenantPilot only'))
        ->assertActionVisible('markFollowUpNeeded')
        ->assertActionExists('markFollowUpNeeded', fn (Action $action): bool => $action->isConfirmationRequired()
            && str_contains((string) $action->getModalDescription(), 'Target state: Follow-up needed')
            && str_contains((string) $action->getModalDescription(), 'TenantPilot only'))
        ->mountAction('markReviewed')
        ->assertActionMounted('markReviewed')
        ->callMountedAction()
        ->assertHasNoActionErrors();

    expect(ManagedEnvironmentTriageReview::query()
        ->where('managed_environment_id', (int) $actionTenant->getKey())
        ->where('concern_family', PortfolioArrivalContextToken::FAMILY_BACKUP_HEALTH)
        ->where('current_state', ManagedEnvironmentTriageReview::STATE_REVIEWED)
        ->whereNull('resolved_at')
        ->exists())->toBeTrue()
        ->and(AuditLog::query()
            ->where('workspace_id', (int) $actionTenant->workspace_id)
            ->where('managed_environment_id', (int) $actionTenant->getKey())
            ->where('action', AuditActionId::ManagedEnvironmentTriageReviewMarkedReviewed->value)
            ->exists())->toBeTrue()
        ->and($component->instance())->toBeInstanceOf(ViewManagedEnvironment::class);
});
