<?php

namespace App\Notifications;

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

class NewVideoUploaded extends Notification
{
    public function __construct(public Video $video, public User $uploader) {}

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

    public function toArray(object $notifiable): array
    {
        return [
            'type'             => 'new_video',
            'video_id'         => $this->video->id,
            'video_route_key'  => $this->video->getRouteKey(),
            'video_title'      => $this->video->title,
            'video_thumbnail'  => $this->video->thumbnail,
            'uploader_id'      => $this->uploader->id,
            'uploader_name'    => $this->uploader->name,
            'uploader_avatar'  => $this->uploader->avatar_url,
        ];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject($this->uploader->name . ' uploaded a new video')
            ->view('emails.new-video-from-sub', [
                'video'    => $this->video,
                'uploader' => $this->uploader,
                'recipient'=> $notifiable,
            ]);
    }
}
