<?php

namespace Lucent\Commands;

use Illuminate\Console\Command;
use Lucent\Channel\ChannelService;
use Lucent\Database\Database;
use Lucent\Edge\EdgeService;
use Lucent\Query\Query;

class RemoveOrphanRecords extends Command
{

    protected $signature = 'lucent:remove-orphan-records {--force}';

    protected $description = 'Searches and removes records without an active schema';


    public function __construct()
    {
        parent::__construct();
    }


    public function handle(ChannelService $channelService): void
    {
        $schemas = $channelService->channel->schemas->pluck("name");
        $count = Database::make()->table("revisions")->whereNotIn("schema",$schemas)->count();
        if (!($this->option('force')  || $this->confirm("$count records found. Delete?"))) {
            return;
        }

        $numOfRowsDeleted = Database::make()->table("records")->whereNotIn("schema",$schemas)->delete();
        Database::make()->table("revisions")->whereNotIn("schema",$schemas)->delete();
        $this->info("$numOfRowsDeleted records were deleted");
    }

}
