<?php

namespace App\Notifications;

use App\Models\Task;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class TaskDueSoonNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public function __construct(public Task $task) {}

    /**
     * @return list<string>
     */
    public function via(object $notifiable): array
    {
        return ['mail', 'database'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject('Task due soon: '.$this->task->title)
            ->line('You have a task due soon.')
            ->line($this->task->title)
            ->when($this->task->due_at, fn (MailMessage $m) => $m->line('Due: '.$this->task->due_at->toDayDateTimeString()))
            ->action('Open tasks', route('tasks.index'));
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(object $notifiable): array
    {
        return [
            'title' => 'Task due soon',
            'message' => $this->task->title,
            'task_id' => $this->task->id,
            'due_at' => $this->task->due_at?->toIso8601String(),
        ];
    }
}
