<?php

declare(strict_types=1);

use App\Filament\Resources\ProviderConnectionResource\Pages\ListProviderConnections;
use App\Models\ManagedEnvironment;
use App\Models\ProviderConnection;
use Livewire\Livewire;

it('uses environment_id query override for authorized environments', function (): void {
    $tenantA = ManagedEnvironment::factory()->create();
    $tenantB = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
    ]);

    [$user] = createUserWithTenant(tenant: $tenantA, role: 'owner');
    createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');

    ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'managed_environment_id' => (int) $tenantA->getKey(),
        'display_name' => 'A Connection',
        'provider' => 'microsoft',
    ]);

    ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantB->workspace_id,
        'managed_environment_id' => (int) $tenantB->getKey(),
        'display_name' => 'B Connection',
        'provider' => 'microsoft',
    ]);

    $this->actingAs($user)
        ->get('/admin/provider-connections?environment_id='.(int) $tenantB->getKey())
        ->assertOk()
        ->assertSee('B Connection')
        ->assertDontSee('A Connection');
});

it('returns not found for unauthorized environment_id query override', function (): void {
    $tenantA = ManagedEnvironment::factory()->create();
    $tenantB = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
    ]);

    [$user] = createUserWithTenant(tenant: $tenantA, role: 'owner');

    ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'managed_environment_id' => (int) $tenantA->getKey(),
        'display_name' => 'A Connection',
        'provider' => 'microsoft',
    ]);

    ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantB->workspace_id,
        'managed_environment_id' => (int) $tenantB->getKey(),
        'display_name' => 'B Connection',
        'provider' => 'microsoft',
    ]);

    $this->actingAs($user)
        ->get('/admin/provider-connections?environment_id='.(int) $tenantB->getKey())
        ->assertNotFound();
});

it('keeps composed list filters inside the authorized tenant override scope', function (): void {
    $tenantA = ManagedEnvironment::factory()->create();
    $tenantB = ManagedEnvironment::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
    ]);

    [$user] = createUserWithTenant(tenant: $tenantA, role: 'owner');
    createUserWithTenant(tenant: $tenantB, user: $user, role: 'owner');

    $tenantAConnection = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
        'managed_environment_id' => (int) $tenantA->getKey(),
        'display_name' => 'A Connected',
        'provider' => 'microsoft',
        'consent_status' => 'granted',
        'verification_status' => 'healthy',
    ]);

    $tenantBConnected = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantB->workspace_id,
        'managed_environment_id' => (int) $tenantB->getKey(),
        'display_name' => 'B Connected',
        'provider' => 'microsoft',
        'consent_status' => 'granted',
        'verification_status' => 'healthy',
    ]);

    $tenantBFailed = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantB->workspace_id,
        'managed_environment_id' => (int) $tenantB->getKey(),
        'display_name' => 'B Failed',
        'provider' => 'microsoft',
        'consent_status' => 'granted',
        'verification_status' => 'error',
    ]);

    $this->actingAs($user);

    Livewire::withQueryParams([
        'environment_id' => (int) $tenantB->getKey(),
    ])->test(ListProviderConnections::class)
        ->filterTable('verification_status', 'healthy')
        ->assertCanSeeTableRecords([$tenantBConnected])
        ->assertCanNotSeeTableRecords([$tenantAConnection, $tenantBFailed]);
});
