<?php

namespace Lucent\Commands;

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

class EmptyTrash extends Command
{

    protected $signature = 'lucent:empty-trash {--force}';

    protected $description = 'Removes all trashed documents';


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


    public function handle(): void
    {
        $trashedIds = Database::make()->table("records")->select("id")->where("status", "trashed")->get()->pluck("id")->toArray();
        $count = count($trashedIds);
        if (!($this->option('force') || $this->confirm("$count trashed records found. Delete?"))) {
            return;
        }

        $numOfRowsDeleted = Database::make()->table("records")->whereIn("id", $trashedIds)->delete();
        Database::make()->table("revisions")->whereIn("recordId", $trashedIds)->delete();
        Database::make()->table("edges")->whereIn("source", $trashedIds)->delete();
        Database::make()->table("edges")->whereIn("target", $trashedIds)->delete();

        $this->info("$numOfRowsDeleted records were deleted");
    }

}
