<?php

declare(strict_types=1);

use App\Models\OperationalControlActivation;
use App\Models\Workspace;
use App\Support\OperationalControls\OperationalControlEvaluator;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(RefreshDatabase::class);

it('returns enabled when no activation matches', function (): void {
    $workspace = Workspace::factory()->create();

    $decision = app(OperationalControlEvaluator::class)->evaluate('restore.execute', $workspace);

    expect($decision->isEnabled())->toBeTrue()
        ->and($decision->effectiveState)->toBe('enabled')
        ->and($decision->scopeLabel())->toBe('No active pause')
        ->and($decision->matchedScopeType)->toBe('none')
        ->and($decision->workspaceId)->toBeNull()
        ->and($decision->reasonText)->toBeNull()
        ->and($decision->sourceActivationId)->toBeNull();
});

it('returns the matching workspace pause when present', function (): void {
    $workspace = Workspace::factory()->create();

    $activation = OperationalControlActivation::factory()->workspaceScoped()->create([
        'control_key' => 'restore.execute',
        'workspace_id' => (int) $workspace->getKey(),
        'reason_text' => 'Restore execution is paused for this workspace.',
    ]);

    $decision = app(OperationalControlEvaluator::class)->evaluate('restore.execute', $workspace);

    expect($decision->isPaused())->toBeTrue()
        ->and($decision->effectiveState)->toBe('paused')
        ->and($decision->hasWorkspaceScope())->toBeTrue()
        ->and($decision->scopeLabel())->toBe('Workspace #'.(int) $workspace->getKey())
        ->and($decision->matchedScopeType)->toBe('workspace')
        ->and($decision->workspaceId)->toBe((int) $workspace->getKey())
        ->and($decision->reasonText)->toBe('Restore execution is paused for this workspace.')
        ->and($decision->sourceActivationId)->toBe((int) $activation->getKey());
});