<?php

namespace App\Notifications;

use App\Models\Communication\Recording;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class VoicemailReceivedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(public readonly Recording $recording) {}

    public function via($notifiable): array
    {
        return ['database', 'broadcast'];
    }

    public function toArray($notifiable): array
    {
        $communication = $this->recording->communication;
        $caller        = $communication?->communicable;
        $callerName    = $caller?->full_name ?? 'Unknown caller';
        $secs          = $this->recording->duration ?? 0;
        $duration      = sprintf('%d:%02d', floor($secs / 60), $secs % 60);

        return [
            'type'             => 'voicemail',
            'title'            => 'New Voicemail',
            'message'          => "From {$callerName} · {$duration}",
            'recording_id'     => $this->recording->id,
            'communication_id' => $communication?->id,
            'client_id'        => $caller?->id,
            'caller_name'      => $callerName,
            'duration'         => $this->recording->duration,
            'solicitor_id'     => $communication?->solicitor_id,
        ];
    }

    public function toBroadcast($notifiable): array
    {
        return $this->toArray($notifiable);
    }
}
