<?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\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;

class VerifyEmail extends Notification implements ShouldQueue
{
    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return MailMessage
     */
    public function toMail($notifiable)
    {
        \App\Components\ClientAdapter::getInstance()
            ->setNameFromNameInDb($this->client_name)
            ->apply();

        $this->verificationUrl = $this->verificationUrl($this->user_id, $this->user_email);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $this->verificationUrl);
        }

        $client_name = $this->client_name;
        return (new MailMessage)
            ->withSwiftMessage([$this, 'addCustomHeaders'])
            //this is unfortunately necessary because the Mailer has already
            // been instantiated and already has the previous settings
            ->from(config('mail.from.address'), config('mail.from.name'))

            ->subject(__("emails.{$client_name}.email-verification.subject"))
            ->greeting(__("emails.{$client_name}.email-verification.greeting"))
            ->line(__("emails.{$client_name}.email-verification.line_1"))
            ->action(__("emails.{$client_name}.email-verification.action_label"), $this->verificationUrl)
            ->line(__("emails.{$client_name}.email-verification.line_2"))
            ->line(__("emails.{$client_name}.email-verification.line_3"))
            ->salutation(__("emails.{$client_name}.email-verification.salutation"));
    }
    /**
     * Add our own headers to the e-mail
     */
    public function addCustomHeaders(\Swift_Message $message)
    {
        $message->getHeaders()->addTextHeader('Auto-Submitted', 'auto-generated');
    }
}
