<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\Middleware\RateLimited;
use Illuminate\Auth\Notifications\VerifyEmail;



class QueuedVerifyEmail extends VerifyEmail implements ShouldQueue
{
  use Queueable, InteractsWithQueue;

  public $timeout = 7200; // 10 ore

  /**
  * Create a new notification instance.
  *
  * @return void
  */
  public function __construct(){
  }


  public function retryUntil(){
    return now()->addHours(5);
  }

  public function middleware(){
    return [new RateLimited('emails')];
  }


  /**
  * Get the notification's delivery channels.
  *
  * @param  mixed  $notifiable
  * @return array
  */
  public function via($notifiable)
  {
    return ['mail'];
  }

  /**
  * Get the mail representation of the notification.
  *
  * @param  mixed  $notifiable
  * @return \Illuminate\Notifications\Messages\MailMessage
  */
  public function toMail($notifiable){
    $verificationUrl = $this->verificationUrl($notifiable);

    return (new MailMessage)
    ->subject('Verifica indirizzo email')
    ->line('Clicca sul pulsante qui sotto per verificare il tuo indirizzo email')
    ->action('Verifica indirizzo email', $verificationUrl);
  }



  /**
  * Get the array representation of the notification.
  *
  * @param  mixed  $notifiable
  * @return array
  */
  public function toArray($notifiable)
  {
    return [
      //
    ];
  }

  // public function handle()
  // {
  //   Redis::throttle('*')->allow(1)->every(1000)->then(function () {
  //     // send email to subscriber
  //     logger($this->email);
  //   }, function () {
  //     // could not obtain lock, retry this job in 5 seconds.
  //     return $this->release(60);
  //   });
  // }
}
