<?php

declare(strict_types=1);

use App\Filament\Pages\Operations\TenantlessOperationRunViewer;
use App\Models\OperationRun;
use App\Notifications\OperationRunCompleted;
use App\Support\Providers\ProviderReasonCodes;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;

uses(RefreshDatabase::class);

function makeProviderBlockedRun(): array
{
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $run = OperationRun::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'user_id' => (int) $user->getKey(),
        'initiator_name' => $user->name,
        'type' => 'inventory_sync',
        'status' => 'completed',
        'outcome' => 'blocked',
        'context' => [
            'provider' => 'microsoft',
            'module' => 'inventory',
            'provider_connection_id' => 999999,
            'reason_code' => ProviderReasonCodes::ProviderConnectionMissing,
            'blocked_by' => 'provider_preflight',
            'target_scope' => [
                'entra_tenant_id' => $tenant->managed_environment_id,
            ],
        ],
    ]);

    return [$user, $tenant, $run];
}

it('reuses translated provider-backed blocker language on the canonical run detail page', function (): void {
    [$user, , $run] = makeProviderBlockedRun();
    $this->actingAs($user);

    $component = Livewire::actingAs($user)->test(TenantlessOperationRunViewer::class, ['run' => $run]);
    $banner = $component->instance()->blockedExecutionBanner();

    expect($banner)->not->toBeNull();
    expect($banner['title'] ?? null)->toBe('Blocked by prerequisite');
    expect($banner['body'] ?? null)->toContain('Provider connection required');
    expect($banner['body'] ?? null)->toContain('usable provider connection');
});

it('keeps terminal notification reason translation aligned with the canonical provider-backed run detail', function (): void {
    [$user, , $run] = makeProviderBlockedRun();
    $this->actingAs($user);

    $banner = Livewire::actingAs($user)
        ->test(TenantlessOperationRunViewer::class, ['run' => $run])
        ->instance()
        ->blockedExecutionBanner();

    $payload = (new OperationRunCompleted($run))->toDatabase($user);

    expect($payload['title'] ?? null)->toBe('Inventory sync blocked by prerequisite');
    expect($payload['body'] ?? null)->toContain('Provider connection required');
    expect($payload['body'] ?? null)->toContain('usable provider connection');
    expect($payload['reason_translation']['operator_label'] ?? null)->toContain('Provider connection required');
    expect($payload['reason_translation']['short_explanation'] ?? null)->toContain('usable provider connection');
    expect($payload['diagnostic_reason_code'] ?? null)->toBe(ProviderReasonCodes::ProviderConnectionMissing);
    expect($banner['body'] ?? '')->toContain('Provider connection required');
});

it('keeps the same blocked provider-backed vocabulary for system-initiated runs on canonical detail', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner', ensureDefaultMicrosoftProviderConnection: false);

    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    $run = OperationRun::factory()->create([
        'workspace_id' => (int) $tenant->workspace_id,
        'managed_environment_id' => (int) $tenant->getKey(),
        'user_id' => null,
        'initiator_name' => 'Scheduled automation',
        'type' => 'inventory_sync',
        'status' => 'completed',
        'outcome' => 'blocked',
        'context' => [
            'provider' => 'microsoft',
            'module' => 'inventory',
            'provider_connection_id' => 999999,
            'reason_code' => ProviderReasonCodes::ProviderConnectionMissing,
            'blocked_by' => 'provider_preflight',
            'target_scope' => [
                'entra_tenant_id' => $tenant->managed_environment_id,
            ],
        ],
    ]);

    $banner = Livewire::actingAs($user)
        ->test(TenantlessOperationRunViewer::class, ['run' => $run])
        ->instance()
        ->blockedExecutionBanner();

    expect($banner)->not->toBeNull();
    expect($banner['title'] ?? null)->toBe('Blocked by prerequisite');
    expect($banner['body'] ?? null)->toContain('Provider connection required');
    expect($banner['body'] ?? null)->toContain('usable provider connection');
});
