<?php

namespace App\Classes\Modules\Accounts\Processors;

use App\Classes\Jobs\EmailVerificationAttemptExpiration;
use App\Classes\Jobs\SendUserVerificationEmail;
use App\Classes\Modules\Accounts\Services\GeneratesEmailVerificationAttempt;
use App\Classes\Modules\Accounts\Services\InvalidatesActiveEmailVerificationAttempts;
use App\Models\User;

class GenerateEmailVerificationAttemptProcessor
{

    /** @var InvalidatesActiveEmailVerificationAttempts */
    private $invalidatesActiveEmailVerificationAttempts;

    /** @var GeneratesEmailVerificationAttempt */
    private $generatesEmailVerificationAttempt;

    /** @var EmailVerificationAttemptExpiration */
    private $emailVerificationAttemptExpiration;

    /** @var SendUserVerificationEmail */
    private $sendUserVerificationEmail;

    /**
     * GenerateEmailVerificationAttemptProcessor constructor.
     * @param InvalidatesActiveEmailVerificationAttempts $invalidatesActiveEmailVerificationAttempts
     * @param GeneratesEmailVerificationAttempt $generatesEmailVerificationAttempt
     * @param EmailVerificationAttemptExpiration $emailVerificationAttemptExpiration
     * @param SendUserVerificationEmail $sendUserVerificationEmail
     */
    public function __construct(InvalidatesActiveEmailVerificationAttempts $invalidatesActiveEmailVerificationAttempts, GeneratesEmailVerificationAttempt $generatesEmailVerificationAttempt, EmailVerificationAttemptExpiration $emailVerificationAttemptExpiration, SendUserVerificationEmail $sendUserVerificationEmail)
    {
        $this->invalidatesActiveEmailVerificationAttempts = $invalidatesActiveEmailVerificationAttempts;
        $this->generatesEmailVerificationAttempt = $generatesEmailVerificationAttempt;
        $this->emailVerificationAttemptExpiration = $emailVerificationAttemptExpiration;
        $this->sendUserVerificationEmail = $sendUserVerificationEmail;
    }


    /**
     * @param User $user
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(User $user) {

        $this->invalidatesActiveEmailVerificationAttempts->execute($user);

        $attempt = $this->generatesEmailVerificationAttempt->execute($user);

        $this->emailVerificationAttemptExpiration::dispatch($attempt)->delay(now()->addHours(48));

        $this->sendUserVerificationEmail::dispatch($user, $attempt);

    }

}
