<?php

namespace Lucent\Commands;

use Illuminate\Console\Command;
use Lucent\Edge\EdgeService;
use Lucent\Query\Query;

class RemoveOrphanEdges extends Command
{

    protected $signature = 'lucent:removeOrphanEdges';

    protected $description = 'Searches and remove orphan edges';


    public function __construct(

    )
    {
        parent::__construct();
    }


    public function handle(EdgeService $edgeService, Query $query): void
    {
        $edges = $edgeService->findAll();

        foreach ($edges as $edge){
            $source = $query->filter(["id" => $edge->source])->run()->records;
            $target = $query->filter(["id" => $edge->target])->run()->records;
            if($source->isEmpty() || $target->isEmpty()){
                $this->info("Edge is orphan");
                $edgeService->remove($edge);
            }

        }
    }

}
