<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;

class VerificationEmail extends Notification implements ShouldQueue
{
    use Queueable;

    protected $_user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->_user = $user;
    }

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

    /**
     * Determine which queues should be used for each notification channel.
     *
     * @return array
     */
    public function viaQueues()
    {
        return [
            'mail' => 'emails'
        ];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $actionURL = URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $this->_user->id,
                'hash' => sha1($this->_user->getEmailForVerification()),
            ]
        );
        return (new MailMessage())
            ->subject('Vérification de votre email')
            ->greeting('Bonjour,')
            ->line('Un compte vous a été créé sur l\'application AptiMap. Merci de bien vouloir cliquer sur le lien de vérification présent dans cet email.')
            ->line('Pensez également à modifier le mot de passe que vous a fourni l\'administrateur de votre structure. Pour ce faire, utilisez la fonctionnalité mot de passe oublié.')
            ->action('Vérifier mon email', $actionURL)
            ->salutation('Merci, ' . Config::get('app.name') . '.');
    }

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