<?php

namespace App\Notifications;

use App\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class NewSubscriberNotification extends Notification
{
    public function __construct(public User $subscriber) {}

    public function via(object $notifiable): array
    {
        $ch = [];
        if ($notifiable->notificationPref('notif_new_subscriber'))   $ch[] = 'database';
        if ($notifiable->notificationPref('email_new_subscriber'))   $ch[] = 'mail';
        return $ch;
    }

    public function toArray(object $notifiable): array
    {
        return [
            'type'              => 'new_subscriber',
            'actor_id'          => $this->subscriber->id,
            'actor_name'        => $this->subscriber->name,
            'actor_avatar'      => $this->subscriber->avatar_url,
            'actor_channel'     => $this->subscriber->channel,
        ];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject($this->subscriber->name . ' subscribed to your channel')
            ->view('emails.new-subscriber', [
                'subscriber' => $this->subscriber,
                'recipient'  => $notifiable,
            ]);
    }
}
