<?php

namespace App\Console\Commands;

use App\Models\AidataMissingfield;
use App\Models\Baradmission;
use App\Models\Candidate;
use App\Models\City;
use App\Models\Country;
use App\Models\Firm;
use App\Models\FirmOffice;
use App\Models\State;
use App\Services\BaradmissionService;
use App\Services\FirmOfficeService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use PhpOffice\PhpSpreadsheet\IOFactory;

class UpdateJdYearFromVoy extends Command
{
    /** /updateJdYear
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update-jd-year';

    protected $octaparseApiService;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'update the addresses of firm offices and mark them reviewed';

    /**
     * Execute the console command.
     */


    public function handle()
    {
         $candidates = $this->getCandidates();
        // echo "<pre>";
        // print_r($candidates->toArray());
        // die;
        if (empty($candidates)) {
            Log::channel('batch_processing')->info('No candidates found to process the jd year');
            return;
        }
        foreach ($candidates as $candidate) {
            $can = DB::table('leopard_imports_main')->where('email', $candidate->email)->first();
            if(!empty($can)) {
                $jd_year = $can->bio;
                if(!empty($jd_year)) {
                    echo $candidate->id;
                    echo "<br>";
                    $candidate->jd_year = $jd_year;
                    $candidate->save();
                    AidataMissingfield::where('candidate_id', $candidate->id)->update(['status' => 1]);
                }
            }
        }
    }

    private function getCandidates() {
        $candidates = array();
        $aiMissingCandidates = AidataMissingfield::where('status',0)
                                            ->where('ai_status',0)
                                            ->whereJsonContains('missing_fields', ['jd_year'])
                                            ->limit(200)
                                            ->inRandomOrder()
                                            ->pluck('candidate_id')
                                            ->toArray();
        if (!empty($aiMissingCandidates)) {
            $candidates = Candidate::select('id', 'first_name', 'last_name', 'jd_year', 'email')
                            ->whereIn('id', $aiMissingCandidates)->get();
        }
        return $candidates;
    }
}
