<?php

use App\Models\InventoryItem;
use App\Models\InventoryLink;
use App\Models\ManagedEnvironment;
use App\Services\Inventory\DependencyQueryService;
use Illuminate\Support\Str;

it('does not leak edges across tenants in service queries', function () {
    $tenantA = ManagedEnvironment::factory()->create();
    $tenantB = ManagedEnvironment::factory()->create();

    /** @var InventoryItem $itemA */
    $itemA = InventoryItem::factory()->create([
        'managed_environment_id' => $tenantA->getKey(),
        'external_id' => (string) Str::uuid(),
    ]);

    // Edge for tenant A
    InventoryLink::factory()->create([
        'managed_environment_id' => $tenantA->getKey(),
        'source_type' => 'inventory_item',
        'source_id' => $itemA->external_id,
        'target_type' => 'foundation_object',
        'target_id' => (string) Str::uuid(),
        'relationship_type' => 'assigned_to',
    ]);

    // Edge for tenant B with same source/target ids but different tenant
    InventoryLink::factory()->create([
        'managed_environment_id' => $tenantB->getKey(),
        'source_type' => 'inventory_item',
        'source_id' => $itemA->external_id,
        'target_type' => 'foundation_object',
        'target_id' => (string) Str::uuid(),
        'relationship_type' => 'assigned_to',
    ]);

    $svc = app(DependencyQueryService::class);
    $outboundA = $svc->getOutboundEdges($itemA);

    expect($outboundA->count())->toBe(1);
});
