<?php

use App\Filament\Resources\ProviderConnectionResource;
use App\Filament\Resources\ProviderConnectionResource\Pages\EditProviderConnection;
use App\Models\OperationRun;
use App\Models\ProviderConnection;
use App\Models\ProviderCredential;
use Filament\Facades\Filament;
use Livewire\Livewire;

it('enables a disabled connection and sets needs_consent when credentials are missing', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

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

    $connection = ProviderConnection::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'provider' => 'microsoft',
        'entra_tenant_id' => fake()->uuid(),
        'status' => 'disabled',
        'health_status' => 'down',
        'last_health_check_at' => now(),
        'last_error_reason_code' => 'provider_auth_failed',
        'last_error_message' => 'Some failure',
    ]);

    Livewire::test(EditProviderConnection::class, ['record' => $connection->getRouteKey()])
        ->callAction('enable_connection');

    $connection->refresh();

    expect($connection->status)->toBe('needs_consent');
    expect($connection->health_status)->toBe('unknown');
    expect($connection->last_health_check_at)->toBeNull();
    expect($connection->last_error_reason_code)->toBeNull();
    expect($connection->last_error_message)->toBeNull();
});

it('enables a disabled connection and sets connected when credentials are present', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');
    $this->actingAs($user);

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

    $connection = ProviderConnection::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'provider' => 'microsoft',
        'entra_tenant_id' => fake()->uuid(),
        'status' => 'disabled',
        'health_status' => 'down',
    ]);

    ProviderCredential::factory()->create([
        'provider_connection_id' => $connection->getKey(),
        'payload' => [
            'client_id' => 'client-id',
            'client_secret' => 'client-secret',
        ],
    ]);

    Livewire::test(EditProviderConnection::class, ['record' => $connection->getRouteKey()])
        ->callAction('enable_connection');

    $connection->refresh();

    expect($connection->status)->toBe('connected');
    expect($connection->health_status)->toBe('unknown');
});

it('shows a link to the last connection check run when present', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $connection = ProviderConnection::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'provider' => 'microsoft',
        'entra_tenant_id' => fake()->uuid(),
    ]);

    $run = OperationRun::factory()->create([
        'tenant_id' => $tenant->getKey(),
        'user_id' => $user->getKey(),
        'initiator_name' => $user->name,
        'type' => 'provider.connection.check',
        'status' => 'completed',
        'outcome' => 'succeeded',
        'context' => [
            'provider' => 'microsoft',
            'module' => 'health_check',
            'provider_connection_id' => (int) $connection->getKey(),
            'target_scope' => [
                'entra_tenant_id' => $connection->entra_tenant_id,
            ],
        ],
    ]);

    $this->actingAs($user)
        ->get(ProviderConnectionResource::getUrl('edit', ['record' => $connection], tenant: $tenant))
        ->assertOk();
});
