<?php

namespace App\Console\Commands;

use App\Helpers\ScraperHelper;
use App\Models\AidataMissingfield;
use App\Models\Candidate;
use App\Models\CandidateAcademic;
use App\Models\CandidateFirmHistory;
use App\Models\CandidateFirmOfficeHistory;
use App\Models\CandidatePracticeAreaHistory;
use App\Models\CandidateTitleHistory;
use App\Models\FirmOffice;
use App\Models\MissingfieldCandidate;
use App\Models\ReviewCandidate;
use App\Models\Title;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class MergeNewCandidate extends Command
{
    /** /mergeCandidates
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:merge-new-candidate';

    protected $octaparseApiService;

    protected $newCandidate;
    protected $existingCandidate;
    protected $allLanguages;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'merge new candidate with the exsiting candidate';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        Log::channel('merge_candidates')->info('Merging started for candidate ID: ');
        $candidates = $this->getNewCandidates();
        $this->allLanguages = ScraperHelper::getAllLanguages();
        if(count($candidates) > 0) {
            foreach ($candidates as $candidate) {
                if($candidate->id == 6901) {
                    continue;
                }
                $this->newCandidate = $candidate;
                $this->existingCandidate = Candidate::with('firmOffices','firmOffices.state','firmOffices.country','firmOffices.city','titles','practiceAreas','matters','news','memberships','accolades','clients','industries','probonos','languages','masterDegreeSubjects','candidateAcademic','baradmissions','clerkships')
                                    ->where('first_name', $candidate->first_name)
                                    ->where('last_name', $candidate->last_name)
                                    ->where('lawschool_id', $candidate->lawschool_id)
                                    ->where('jd_year', $candidate->jd_year)
                                    ->where('scraped_status','!=' ,config('constants.status.scrape.new'))
                                    ->where('scraped_status','!=' ,config('constants.status.scrape.updated_and_new'))
                                    ->first();
                if ($this->existingCandidate) {
                    $this->existingCandidate->id;
                    $this->newCandidate->id;
                    if($this->newCandidate->firm_id == $this->existingCandidate->firm_id) {
                        echo "both candidates are in same firm";
                    }
                    echo "why here";
                    die;
                    $this->info("Merging candidates");
                    $this->mergeCandidates();
                    $this->removeCandidateFromReviews();
                    $this->initialScrapeMissingFieldReview();  // intial scrape missing field review
                   // $this->aiDataMissingFieldReview();    // missing data from OPEN AI data updation
                    $candidate->delete();
                } else {
                    $this->updateCandidateStatus($candidate);
                }
            }
        } else {
            $this->info('No new candidates found to merge.');
        }
    }

    private function getNewCandidates()
    {
        // avoid candidates that are already in the missing field review
        $missingaireviewCandidateIds = AidataMissingfield::where('status', 0)
                        ->orWhere(function ($query){
                            $query->where('status',1)
                            ->where('updated_at', '>', Carbon::now()->subDay());
                        })->pluck('candidate_id')->toArray();

        // avoid candidates with title in review
        $missingReviewCandidateIds = Candidate::whereHas('titles', function ($q) {
                            $q->where('status', 0);
                        })
                        ->orwhereHas('firmOffices', function ($q) {
                            $q->where('status', 0);
                        })
                        ->orwhereHas('baradmissions', function ($q) {
                            $q->where('status', 0);
                        })
                        ->orwhereHas('languages', function ($q) {
                            $q->where('status', 0);
                        })
                        ->orwhereHas('clerkships', function ($q) {
                            $q->where('status', 0);
                        })
                        ->orwhereHas('lawschool', function ($q) {
                            $q->where('status', 0);
                        })
                        ->pluck('id')->toArray();

        $candidateIds = array_merge($missingaireviewCandidateIds,$missingReviewCandidateIds);

        return Candidate::with('firmOffices','firmOffices.state','firmOffices.country','firmOffices.city','titles','practiceAreas','matters','news','memberships','accolades','clients','industries','probonos','languages','masterDegreeSubjects','candidateAcademic','baradmissions','clerkships')
            ->where('status',config('constants.status.default.inactive'))
            ->where('scraped_status',config('constants.status.scrape.updated_and_new'))
            ->whereNotIn('id',$candidateIds)
            ->limit(50)
            ->get();
    }

    private function updateCandidateStatus()
    {
        $this->newCandidate->scraped_status = config('constants.status.scrape.updated');
        $this->newCandidate->status = config('constants.status.default.active');
        $this->newCandidate->save();
    }

    private function mergeCandidates()
    {
        // if value in new candidate, then save new candidate values and keeps the old ones too
        $this->saveNews();
        $this->saveMatters();
        $this->saveMemberships();
        $this->saveAccolades();
        $this->saveClients();
        $this->saveIndustries();
        $this->saveProbonos();
        $this->saveLawjournals();

        $this->saveThoughtLeaderships();
        $this->saveAdverseParties();
        $this->saveAffinitygroups();
        $this->saveClerkshipwithjudges();

        // if no value in existing candidate, then save new candidate values
        $this->saveMasterDegreeSubjects();
        $this->saveLawschoolhonors();
        $this->saveUndergraduateSpecialty();

        // if value in new candidate, then save new candidate values and remove the old ones
        $this->updateLanguages();
        $this->updateClerkships();

        // add new ones and mark old ones as possible ones (ones that are common)
        $this->updateBarAdmissions();

        // add the new ones and old ones if any to historical data
        $this->updatePracticeAreas();

        // add the new ones and old ones if any to historical data and check promotion
        $this->updateTitles();

        // add the new ones and old ones if any to historical data and check for geographic move
        $this->updateFirmOffices();

        $this->updateFirmHistories();
        $this->updateAcademics();

        $lastLateralMove = $this->existingCandidate->last_lateral_move;
        if($this->newCandidate->firm_id != $this->existingCandidate->firm_id) {
            $lastLateralMove = Carbon::now()->format('Y-m-d');
        }

        Candidate::where('id',$this->existingCandidate->id)
                ->update([
                    'firm_id' => $this->newCandidate->firm_id,
                    'previous_firm_id' => $this->existingCandidate->firm_id,
                    'scraped_name' => $this->newCandidate->scraped_name,
                    'scraped_location' => $this->newCandidate->scraped_location,
                    'scraped_title' => $this->newCandidate->scraped_title,
                    'middle_name' => $this->newCandidate->middle_name,
                    'email' => $this->newCandidate->email,
                    'home_phone' => $this->newCandidate->home_phone,
                    'work_phone' => $this->newCandidate->work_phone,
                    'address' => $this->newCandidate->address,
                    'zipcode' => $this->newCandidate->zipcode,
                    'city_id' => $this->newCandidate->city_id,
                    'state_id' => $this->newCandidate->state_id,
                    'country_id' => $this->newCandidate->country_id,
                    'validation' => 0,
                    'url' => $this->newCandidate->url,
                    'linkedIn_url' => $this->newCandidate->linkedIn_url,
                    'bio' => $this->newCandidate->bio,
                    'last_lateral_move' => $lastLateralMove,
                    'scraped_status' => config('constants.status.scrape.updated'),
                    'status' => config('constants.status.default.active'),
                ]);
    }


    private function saveNews()
    {
        if (count($this->newCandidate->news) > 0) {
            $data = array_unique($this->newCandidate->news->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->news()->createMany($formattedData);
        }
    }

    private function saveThoughtLeaderships()
    {
        if (count($this->newCandidate->thoughtLeaderships) > 0) {
            $data = array_unique($this->newCandidate->thoughtLeaderships->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->thoughtLeaderships()->createMany($formattedData);
        }
    }

    private function saveAdverseParties()
    {
        if (count($this->newCandidate->adversParties) > 0) {
            $data = array_unique($this->newCandidate->adversParties->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->adversParties()->createMany($formattedData);
        }
    }

    private function saveAffinitygroups()
    {
        if (count($this->newCandidate->affinitygroups) > 0) {
            $data = array_unique($this->newCandidate->affinitygroups->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->affinitygroups()->createMany($formattedData);
        }
    }

    private function saveClerkshipwithjudges()
    {
        if (count($this->newCandidate->clerkshipwithjudges) > 0) {
            $data = array_unique($this->newCandidate->clerkshipwithjudges->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->clerkshipwithjudges()->createMany($formattedData);
        }
    }

    private function saveMatters()
    {
        if (count($this->newCandidate->matters) > 0) {
            $data = array_unique($this->newCandidate->matters->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->matters()->createMany($formattedData);
        }
    }

    private function saveMemberships()
    {
        if (count($this->newCandidate->memberships) > 0) {
            $data = array_unique($this->newCandidate->memberships->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->memberships()->createMany($formattedData);
        }
    }

    private function saveAccolades()
    {
        if (count($this->newCandidate->accolades) > 0) {
            $data = array_unique($this->newCandidate->accolades->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->accolades()->createMany($formattedData);
        }
    }

    private function saveClients()
    {
        if (count($this->newCandidate->clients) > 0) {
            $data = array_unique($this->newCandidate->clients->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->clients()->createMany($formattedData);
        }
    }

    private function saveIndustries()
    {
        if (count($this->newCandidate->industries) > 0) {
            $data = array_unique($this->newCandidate->industries->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->industries()->createMany($formattedData);
        }
    }

    private function saveProbonos()
    {
        if (count($this->newCandidate->probonos) > 0) {
            $data = array_unique($this->newCandidate->probonos->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->probonos()->createMany($formattedData);
        }
    }

    private function saveLawjournals()
    {
        if (count($this->newCandidate->lawjournals) > 0) {
            $data = array_unique($this->newCandidate->lawjournals->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item,'associated_firm_id'=>$this->newCandidate->firm_id], $data);
            $this->existingCandidate->lawjournals()->createMany($formattedData);
        }
    }

    private function saveMasterDegreeSubjects()
    {
        if (count($this->existingCandidate->masterDegreeSubjects) == 0) {
            $data = array_unique($this->newCandidate->masterDegreeSubjects->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item], $data);
            $this->existingCandidate->masterDegreeSubjects()->createMany($formattedData);
        }
    }

    private function saveLawschoolhonors()
    {
        if (count($this->newCandidate->lawschoolhonors) > 0) {
            $lawschoolHonorIds = array();
            foreach($this->newCandidate->lawschoolhonors as $lawschoolhonor) {
                $lawschoolHonorIds[] = $lawschoolhonor->id;
            }
            if (is_array($lawschoolHonorIds) && sizeof($lawschoolHonorIds) > 0) {
                $this->existingCandidate->lawschoolhonors()->detach();
                $this->existingCandidate->lawschoolhonors()->attach($lawschoolHonorIds);
            }
        }
    }

    private function saveUndergraduateSpecialty()
    {
        if (count($this->existingCandidate->undergradspecialities) == 0) {
            $data = array_unique($this->newCandidate->undergradspecialities->pluck('name')->toArray());
            $formattedData = array_map(fn($item) => ['name' => $item], $data);
            $this->existingCandidate->undergradspecialities()->createMany($formattedData);
        }
    }

    private function updateLanguages()
    {
        if (count($this->newCandidate->languages) > 0) {
            $languageIds = array();
            foreach($this->newCandidate->languages as $language) {
                $languageIds[] = $language->id;
            }
            if (is_array($languageIds) && sizeof($languageIds) > 0) {
                $this->existingCandidate->languages()->detach();
                $this->existingCandidate->languages()->attach($languageIds);
            }
        }
    }

    private function updateClerkships()
    {
        if (count($this->newCandidate->clerkships) > 0) {
            $clerkshipIds = array();
            foreach($this->newCandidate->clerkships as $clerkship) {
                $clerkshipIds[] = $clerkship->id;
            }
            if (is_array($clerkshipIds) && sizeof($clerkshipIds) > 0) {
                $this->existingCandidate->clerkships()->detach();
                $this->existingCandidate->clerkships()->attach($clerkshipIds);
            }
        }
    }

    private function updateBarAdmissions()
    {
        $existingBaradmissionIds = array();
        if(count($this->existingCandidate->baradmissions) > 0) {
            foreach($this->existingCandidate->baradmissions as $baradmission) {
                $existingBaradmissionIds[] = $baradmission->id;
            }
        }
        if (count($this->newCandidate->baradmissions) > 0) {
            $newBaradmissionIds = array();
            foreach($this->newCandidate->baradmissions as $baradmission) {
               $newBaradmissionIds[] = $baradmission->id;
               $toBeAddedBardIds[$baradmission->id] = ['previous' => 0];
            }
            $previousBarIds = array_diff($existingBaradmissionIds, $newBaradmissionIds);
            if(count($previousBarIds) > 0) {
                foreach($previousBarIds as $barId) {
                    $toBeAddedBardIds[$barId] =['previous' => 1];
                }
            }
            $this->existingCandidate->baradmissions()->detach();
            $this->existingCandidate->baradmissions()->attach($toBeAddedBardIds);
        } else {
            if(count($existingBaradmissionIds) > 0) {
                $toBeAddedBardIds = array();
                foreach($existingBaradmissionIds as $barId) {
                    $toBeAddedBardIds[$barId] =  ['previous' => 1];
                }
                $this->existingCandidate->baradmissions()->detach();
                $this->existingCandidate->baradmissions()->attach($toBeAddedBardIds);
            }
        }
    }

    private function updatePracticeAreas()
    {
        $newPracticeIds = array();
        $existingPracticeIds = array();

        if (count($this->newCandidate->practiceAreas) > 0) {
            foreach($this->newCandidate->practiceAreas as $practice) {
                $newPracticeIds[] = $practice->id;
            }
        }
        if (count($this->existingCandidate->practiceAreas) > 0) {
            foreach($this->existingCandidate->practiceAreas as $practice) {
                $existingPracticeIds[] = $practice->id;
            }
        }

        if(count($newPracticeIds) > 0) {
            $this->existingCandidate->practiceAreas()->detach();
            $this->existingCandidate->practiceAreas()->attach($newPracticeIds);
        }

        if(count($existingPracticeIds) > 0) {
            $latestVersion = CandidatePracticeAreaHistory::where('candidate_id', $this->existingCandidate->id)->max('version');
            $newVersion = $latestVersion ? $latestVersion + 1 : 1;
            foreach($existingPracticeIds as $practiceArea) {
                CandidatePracticeAreaHistory::create([
                    'candidate_id' => $this->existingCandidate->id,
                    'practice_area_id'=>$practiceArea,
                    'version' => $newVersion,
                    'changed_at' => now(),
                ]);
            }
        }
    }

    private function updateTitles()
    {
        $newTitleIds = array();
        $oldTitleIds = array();
        if(count($this->newCandidate->titles) > 0) {
            foreach($this->newCandidate->titles as $title) {
                $newTitleIds[] = $title->id;
            }
        }
        if(count($this->existingCandidate->titles) > 0) {
            foreach($this->existingCandidate->titles as $title) {
                $oldTitleIds[] = $title->id;
            }
        }

        $allTitleIds = array_merge($oldTitleIds, $newTitleIds);
        $reviewTitle = Title::whereIn('id',$allTitleIds)->where('status',config('constants.status.titles.under_review'))->exists();
        if(!$reviewTitle) {
            $removedTitles = array_diff($oldTitleIds, $newTitleIds);
            $addedTitles = array_diff($newTitleIds, $oldTitleIds);
            if(count($newTitleIds) > 0 && count($oldTitleIds) > 0) {
                if (count($removedTitles) > 0 || count($addedTitles) > 0) {
                    $this->checkForPromotion($oldTitleIds, $newTitleIds);
                }
            }
        }
        $this->existingCandidate->titles()->detach();
        if (count($newTitleIds) > 0) {
            $this->existingCandidate->titles()->attach($newTitleIds);
        }

        if(count($oldTitleIds) > 0) {
            $latestVersion = CandidateTitleHistory::where('candidate_id', $this->existingCandidate->id)->max('version');
            $newVersion = $latestVersion ? $latestVersion + 1 : 1;
            foreach($oldTitleIds as $oldTitleId) {
                CandidateTitleHistory::create([
                    'candidate_id' => $this->existingCandidate->id,
                    'title_id'=>$oldTitleId,
                    'version' => $newVersion,
                    'changed_at' => now(),
                ]);
            }
        }
    }

    public function checkForPromotion($oldTitleIds, $newTitleIds)
    {
        $associateId = 1;
        $partnerId = 7;
        $counselId = 3;
        $oldTitle = '';
        $newTitle = '';
        if(in_array($associateId,$oldTitleIds)) {
            $oldTitle = 1;
        }
        if(in_array($counselId,$oldTitleIds)) {
            $oldTitle = 2;
        }
        if(in_array($partnerId,$oldTitleIds)) {
            $oldTitle = 3;
        }
        if(in_array($associateId,$newTitleIds)) {
            $newTitle = 1;
        }
        if(in_array($counselId,$newTitleIds)) {
            $newTitle = 2;
        }
        if(in_array($partnerId,$newTitleIds)) {
            $newTitle = 3;
        }
        if(!empty($oldTitle) && !empty($newTitle)) {
            if ($oldTitle < $newTitle) {
                $this->existingCandidate->promotion_date = now();
                $this->existingCandidate->save();
            }
        }
    }

    private function updateFirmOffices()
    {
        $newCountries = array();
        $newCities = array();
        $newStates = array();
        $newOfficeIds = array();
        if(!empty($this->newCandidate->firmOffices)) {
            foreach($this->newCandidate->firmOffices as $office) {
                $newOfficeIds[] = $office->id;
                $newCountries[] = $office->country_id;
                if(empty($office->state_id)) {
                    $newStates[] = strtolower($office->state_name);
                } else {
                    $newStates[] = strtolower($office->state->name);
                }
                if(empty($office->city_id)) {
                    $newCities[] = strtolower($office->city_name);
                } else {
                    $newCities[] = strtolower($office->city->name);
                }
            }
        }

        $existingCountries = array();
        $existingCities = array();
        $existingStates = array();
        $existingOfficeIds = array();
        if(!empty($this->existingCandidate->firmOffices)) {
            foreach($this->existingCandidate->firmOffices as $office) {
                $existingCountries[] = $office->country_id;
                $existingOfficeIds[] = $office->id;
                if(empty($office->state_id)) {
                    $existingStates[] = strtolower($office->state_name);
                } else {
                    $existingStates[] = strtolower($office->state->name);
                }
                if(empty($office->city_id)) {
                    $existingCities[] = strtolower($office->city_name);
                } else {
                    $existingCities[] = strtolower($office->city->name);
                }
            }
        }

        if(count($existingOfficeIds) > 0) {
            $latestVersion = CandidateFirmOfficeHistory::where('candidate_id', $this->existingCandidate->id)->max('version');
            $newVersion = $latestVersion ? $latestVersion + 1 : 1;
            foreach($existingOfficeIds as $existingOfficeId) {
                CandidateFirmOfficeHistory::create([
                    'candidate_id' => $this->existingCandidate->id,
                    'firm_office_id'=>$existingOfficeId,
                    'version' => $newVersion,
                    'changed_at' => now(),
                ]);
            }
        }

        $this->existingCandidate->firmOffices()->detach();
        if (is_array($newOfficeIds) && sizeof($newOfficeIds) > 0) {
            $this->existingCandidate->firmOffices()->attach($newOfficeIds);
        }

        $allOfficeIds = array_merge($newOfficeIds, $existingOfficeIds);
        $reviewOffice = FirmOffice::whereIn('id',$allOfficeIds)->where('status',0)->exists();
        if(!$reviewOffice) {
            $addedCountries = array_diff($newCountries, $existingCountries);
            $removedCountries = array_diff($existingCountries, $newCountries);
            if (count($addedCountries) > 0 || count($removedCountries) > 0) {
                $this->existingCandidate->geographic_move = now();
                $this->existingCandidate->save();
            } else {
                $addedStates= array_diff($newStates, $existingStates);
                $removedStates = array_diff($existingStates, $newStates);
                if (count($addedStates) > 0 || count($removedStates) > 0) {
                    $this->existingCandidate->geographic_move = now();
                    $this->existingCandidate->save();
                } else {
                    $addedCities= array_diff($newCities, $existingCities);
                    $removedCities = array_diff($existingCities, $newCities);
                    if (count($addedCities) > 0 || count($removedCities) > 0) {
                        $this->existingCandidate->geographic_move = now();
                        $this->existingCandidate->save();
                    }
                }
            }
        }
    }

    private function updateAcademics()
    {
        $existingAcademics = $this->existingCandidate->candidateAcademic;
        $newAcademics = $this->newCandidate->candidateAcademic;

        $data = ['candidate_id' => $this->existingCandidate->id];
        $values = [
                'undergraduate_school' => empty($existingAcademics->undergraduate_school) ? $newAcademics->undergraduate_school : $existingAcademics->undergraduate_school,
                'undergraduate_school_graduation_year' => empty($existingAcademics->undergraduate_school_graduation_year) ? $newAcademics->undergraduate_school_graduation_year : $existingAcademics->undergraduate_school_graduation_year,
                'phd_school' => empty($existingAcademics->phd_school) ? $newAcademics->phd_school : $existingAcademics->phd_school,
                'phd_subject' => empty($existingAcademics->phd_subject) ? $newAcademics->phd_subject : $existingAcademics->phd_subject,
                'master_degree_school' => empty($existingAcademics->master_degree_school) ? $newAcademics->master_degree_school : $existingAcademics->master_degree_school,
                'master_degree_subject' => empty($existingAcademics->master_degree_subject) ? $newAcademics->master_degree_subject : $existingAcademics->master_degree_subject,
                'mba_school' => empty($existingAcademics->mba_school) ? $newAcademics->mba_school : $existingAcademics->mba_school,
                'md_school' => empty($existingAcademics->md_school) ? $newAcademics->md_school : $existingAcademics->md_school,
                'llm_in_tax_law_school' => empty($existingAcademics->llm_in_tax_law_school) ? $newAcademics->llm_in_tax_law_school : $existingAcademics->llm_in_tax_law_school,
                'other_llm_school' => empty($existingAcademics->other_llm_school) ? $newAcademics->other_llm_school : $existingAcademics->other_llm_school
            ];

        CandidateAcademic::updateOrCreate($data, $values);
    }

    private function updateFirmHistories()
    {
        $latestVersion = CandidateFirmHistory::where('candidate_id', $this->existingCandidate->id)->max('version');
        $newVersion = $latestVersion ? $latestVersion + 1 : 1;
        CandidateFirmHistory::create([
            'candidate_id' => $this->existingCandidate->id,
            'firm_id'=>$this->existingCandidate->firm_id,
            'version' => $newVersion,
            'changed_at' => now(),
        ]);
    }

    private function initialScrapeMissingFieldReview()
    {
        $existInMissingField = MissingfieldCandidate::where('candidate_id', $this->newCandidate->id)->first();
        if ($existInMissingField) {
            MissingfieldCandidate::create([
                'candidate_id' => $this->existingCandidate->id,
                'field_empty'=>$this->newCandidate->field_empty,
                'reviewed_by' => $this->newCandidate->reviewed_by
            ]);
        }
    }

    private function removeCandidateFromReviews()
    {
        ReviewCandidate::where('id','=', $this->existingCandidate->id)->delete(); // remove from NAT review
        AidataMissingfield::where('candidate_id', '=', $this->existingCandidate->id)->delete();
        MissingfieldCandidate::where('candidate_id', '=', $this->existingCandidate->id)->delete();
    }

    private function aiDataMissingFieldReview()
    {
        $aidataMissing = false;
        AidataMissingfield::where('candidate_id', $this->newCandidate->id)->delete();
        if($this->newCandidate->jd_year == '' && $this->existingCandidate->jd_year == '') {
            $aidataMissing = true;
        }
        if($this->newCandidate->lawschool_id == '' && $this->existingCandidate->lawschool_id == '') {
            $aidataMissing = true;
        }
        if($this->newCandidate->email == '') {
            $aidataMissing = true;
        }
        if($aidataMissing) {
            AidataMissingfield::create([
                'candidate_id' => $this->existingCandidate->id
            ]);
        }
    }
}
