<?php

namespace App\Notifications;

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

class NewCommentNotification extends Notification
{
    public function __construct(
        public Video $video,
        public Comment $comment,
        public User $commenter
    ) {}

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

    public function toArray(object $notifiable): array
    {
        return [
            'type'             => 'new_comment',
            'video_id'         => $this->video->id,
            'video_route_key'  => $this->video->getRouteKey(),
            'video_title'      => $this->video->title,
            'video_thumbnail'  => $this->video->thumbnail,
            'actor_id'         => $this->commenter->id,
            'actor_name'       => $this->commenter->name,
            'actor_avatar'     => $this->commenter->avatar_url,
            'comment_preview'  => Str::limit($this->comment->body, 80),
        ];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject($this->commenter->name . ' commented on your video')
            ->view('emails.new-comment', [
                'video'    => $this->video,
                'comment'  => $this->comment,
                'commenter'=> $this->commenter,
                'recipient'=> $notifiable,
            ]);
    }
}
