<?php

declare(strict_types=1);

namespace App\Mail;

use App\Models\Reminder;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

/**
 * Email-уведомление о наступлении срока напоминания (ТЗ §18.5, событие reminder).
 *
 * Триггер: cron-команда `reminders:dispatch-due` находит rows с
 * `is_sent=false AND completed_at IS NULL AND remind_at <= NOW()`,
 * вызывает NotificationService::notifyReminder для каждой,
 * затем ставит `is_sent=true, sent_at=NOW()`.
 */
class ReminderDueNotification extends Mailable
{
    use Queueable;
    use SerializesModels;

    public function __construct(
        public User $recipient,
        public Reminder $reminder,
    ) {}

    public function envelope(): Envelope
    {
        $shortText = $this->reminder->text
            ? mb_substr($this->reminder->text, 0, 60)
            : 'Срок касания клиента';

        return new Envelope(
            subject: "Лидерра. Напоминание — {$shortText}",
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.reminder',
            with: [
                'recipient' => $this->recipient,
                'reminder' => $this->reminder,
            ],
        );
    }
}
