<?php

namespace App\Classes\Jobs\Commands\V2;

use App\Classes\Modules\Accounts\Services\ExpiresEmailVerificationAttempt;
use App\Models\UserEmailVerification;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class NewUserRegistrationExpireCheckV2CommandJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        Log::info(Carbon::now() . ': Start job - New user email verification expiration check.');
        $start   = new Carbon();

        $attempts = UserEmailVerification::active()->twoDaysOld()->get();

        Log::info('Carbon now()->subHours(48): '. Carbon::now()->subHours(48));
        Log::info('Attempts count: '.count($attempts));

        foreach ($attempts as $attempt){
            (new ExpiresEmailVerificationAttempt())->execute($attempt);
            Log::info('Expired: '.$attempt->email.', '.$attempt->created_at);
        }

        $end = new Carbon();
        $elapsedTime = $start->diff($end)->format('%H:%I:%S');
        Log::info(Carbon::now() . ': End job - New user email verification expiration check. ElapsedTime: ' . $elapsedTime . '.');
    }
}
