<?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 NewReplyNotification extends Notification
{
    public function __construct(
        public Video $video,
        public Comment $reply,
        public User $replier
    ) {}

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

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

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