<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class SystemEventNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $tries = 3;
    public $backoff = [60, 300, 900];

    protected $subject;
    protected $message;
    protected $url;
    protected $metadata;

    public function __construct($subject, $message, $url = null, array $metadata = [])
    {
        $this->subject = $subject;
        $this->message = $message;
        $this->url = $url;
        $this->metadata = $metadata;
    }

    public function via($notifiable)
    {
        $channels = ['database'];

        if (!empty($notifiable->email)) {
            $channels[] = 'mail';
        }

        return $channels;
    }

    public function toMail($notifiable)
    {
        $mail = (new MailMessage)
            ->subject($this->subject)
            ->line($this->message);

        if ($this->url) {
            $mail->action('View Details', $this->url);
        }

        return $mail;
    }

    public function toArray($notifiable)
    {
        return [
            'subject' => $this->subject,
            'message' => $this->message,
            'url' => $this->url,
            'metadata' => $this->metadata,
        ];
    }
}
