<?php

namespace App\Services\Inventory;

use App\Models\InventoryItem;
use App\Models\InventoryLink;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;

class DependencyQueryService
{
    public function getOutboundEdges(InventoryItem $item, ?string $relationshipType = null, int $limit = 50): EloquentCollection
    {
        $query = InventoryLink::query()
            ->where('managed_environment_id', $item->managed_environment_id)
            ->where('source_type', 'inventory_item')
            ->where('source_id', $item->external_id)
            ->orderByDesc('created_at');

        if ($relationshipType !== null) {
            $query->where('relationship_type', $relationshipType);
        }

        return $query->limit($limit)->get();
    }

    public function getInboundEdges(InventoryItem $item, ?string $relationshipType = null, int $limit = 50): EloquentCollection
    {
        $query = InventoryLink::query()
            ->where('managed_environment_id', $item->managed_environment_id)
            ->where('target_type', 'inventory_item')
            ->where('target_id', $item->external_id)
            ->orderByDesc('created_at');

        if ($relationshipType !== null) {
            $query->where('relationship_type', $relationshipType);
        }

        return $query->limit($limit)->get();
    }
}
