<?php

namespace App\Console\Commands;

use App\Models\ImportPost;
use App\Services\ImportService;
use Illuminate\Console\Command;

class ImportUploadGarbageCollection extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:import-upload-garbage-collection';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        if (! config('import.instagram.enabled')) {
            return;
        }

        $ips = ImportPost::whereNull('status_id')->where('skip_missing_media', true)->take(100)->get();

        if (! $ips->count()) {
            return;
        }

        foreach ($ips as $ip) {
            $pid = $ip->profile_id;
            $ip->delete();
            ImportService::getPostCount($pid, true);
            ImportService::clearAttempts($pid);
            ImportService::getImportedFiles($pid, true);
        }
    }
}
