<?php

declare(strict_types=1);

use App\Livewire\InventoryItemDependencyEdgesTable;
use App\Models\InventoryItem;
use App\Models\InventoryLink;
use App\Models\ManagedEnvironment;
use App\Models\User;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Livewire\Livewire;

uses(RefreshDatabase::class);

function dependencyEdgesTableComponent(User $user, ManagedEnvironment $tenant, InventoryItem $item)
{
    $tenant->makeCurrent();
    Filament::setTenant($tenant, true);

    test()->actingAs($user);

    return Livewire::actingAs($user)->test(InventoryItemDependencyEdgesTable::class, [
        'inventoryItemId' => (int) $item->getKey(),
    ]);
}

it('renders dependency rows through native table filters and preserves missing-target hints', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $item = InventoryItem::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'external_id' => (string) Str::uuid(),
    ]);

    $assigned = InventoryLink::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'source_type' => 'inventory_item',
        'source_id' => $item->external_id,
        'target_type' => 'missing',
        'target_id' => null,
        'relationship_type' => 'assigned_to',
        'metadata' => [
            'last_known_name' => 'Assigned Target',
            'raw_ref' => ['example' => 'assigned'],
        ],
    ]);

    $scoped = InventoryLink::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'source_type' => 'inventory_item',
        'source_id' => $item->external_id,
        'target_type' => 'missing',
        'target_id' => null,
        'relationship_type' => 'scoped_by',
        'metadata' => [
            'last_known_name' => 'Scoped Target',
            'raw_ref' => ['example' => 'scoped'],
        ],
    ]);

    $inbound = InventoryLink::factory()->create([
        'managed_environment_id' => (int) $tenant->getKey(),
        'source_type' => 'inventory_item',
        'source_id' => (string) Str::uuid(),
        'target_type' => 'inventory_item',
        'target_id' => $item->external_id,
        'relationship_type' => 'depends_on',
    ]);

    $component = dependencyEdgesTableComponent($user, $tenant, $item)
        ->assertTableFilterExists('direction')
        ->assertTableFilterExists('relationship_type')
        ->assertCanSeeTableRecords([
            (string) $assigned->getKey(),
            (string) $scoped->getKey(),
            (string) $inbound->getKey(),
        ])
        ->assertSee('Assigned Target')
        ->assertSee('Scoped Target')
        ->assertSee('Missing');

    $component
        ->filterTable('direction', 'outbound')
        ->assertCanSeeTableRecords([
            (string) $assigned->getKey(),
            (string) $scoped->getKey(),
        ])
        ->assertCanNotSeeTableRecords([(string) $inbound->getKey()])
        ->removeTableFilters()
        ->filterTable('direction', 'inbound')
        ->assertCanSeeTableRecords([(string) $inbound->getKey()])
        ->assertCanNotSeeTableRecords([
            (string) $assigned->getKey(),
            (string) $scoped->getKey(),
        ])
        ->removeTableFilters()
        ->filterTable('relationship_type', 'scoped_by')
        ->assertCanSeeTableRecords([(string) $scoped->getKey()])
        ->assertCanNotSeeTableRecords([
            (string) $assigned->getKey(),
            (string) $inbound->getKey(),
        ])
        ->removeTableFilters()
        ->filterTable('direction', 'outbound')
        ->filterTable('relationship_type', 'depends_on')
        ->assertCountTableRecords(0)
        ->assertSee('No dependencies found');
});

it('returns deny-as-not-found when mounted for an item outside the current tenant scope', function (): void {
    [$user, $tenant] = createUserWithTenant(role: 'owner');

    $foreignItem = InventoryItem::factory()->create([
        'managed_environment_id' => (int) ManagedEnvironment::factory()->create()->getKey(),
        'external_id' => (string) Str::uuid(),
    ]);

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

    $component = Livewire::actingAs($user)->test(InventoryItemDependencyEdgesTable::class, [
        'inventoryItemId' => (int) $foreignItem->getKey(),
    ]);

    $component->assertSee('Not Found');

    expect($component->instance())->toBeNull();
});
