<?php

declare(strict_types=1);

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

it('uses tenant_id query override for authorized tenants', function (): void {
    $tenantA = Tenant::factory()->create();
    $tenantB = Tenant::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,
        'tenant_id' => (int) $tenantA->getKey(),
        'display_name' => 'A Connection',
        'provider' => 'microsoft',
    ]);

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

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

it('returns empty list for unauthorized tenant_id query override', function (): void {
    $tenantA = Tenant::factory()->create();
    $tenantB = Tenant::factory()->create([
        'workspace_id' => (int) $tenantA->workspace_id,
    ]);

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

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

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

    $this->actingAs($user)
        ->get('/admin/provider-connections?tenant_id='.(string) $tenantB->external_id)
        ->assertOk()
        ->assertDontSee('A Connection')
        ->assertDontSee('B Connection');
});

it('keeps composed list filters inside the authorized tenant override scope', function (): void {
    $tenantA = Tenant::factory()->create();
    $tenantB = Tenant::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,
        'tenant_id' => (int) $tenantA->getKey(),
        'display_name' => 'A Connected',
        'provider' => 'microsoft',
        'status' => 'connected',
    ]);

    $tenantBConnected = ProviderConnection::factory()->create([
        'workspace_id' => (int) $tenantB->workspace_id,
        'tenant_id' => (int) $tenantB->getKey(),
        'display_name' => 'B Connected',
        'provider' => 'microsoft',
        'status' => 'connected',
    ]);

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

    $this->actingAs($user);

    Livewire::withQueryParams([
        'tenant_id' => (string) $tenantB->external_id,
    ])->test(ListProviderConnections::class)
        ->filterTable('status', 'connected')
        ->assertCanSeeTableRecords([$tenantBConnected])
        ->assertCanNotSeeTableRecords([$tenantAConnection, $tenantBFailed]);
});
