<?php

namespace App\Classes\Jobs\Commands\V2;

use App\Classes\Modules\Accounts\Services\ExpiresPasswordReset;
use App\Models\PasswordReset;
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 PasswordResetTokenExpirationV2CommandJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function handle()
    {
        Log::info(Carbon::now() . ': Start job - Password reset token expiration check.');
        $start   = new Carbon();

        $attempts = PasswordReset::active()->oneDayOld()->get();

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

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

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