<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class RegistrationVerificationMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public int $tries = 3;

    public int $timeout = 30;

    public array $backoff = [60, 300, 900];

    public function __construct(public readonly string $token)
    {
        $this->onQueue('mail');
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Welcome to Skinbase — confirm your email',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.registration-verification',
            with: [
                'verificationUrl' => url('/verify/'.$this->token),
            ],
        );
    }

    public function attachments(): array
    {
        return [];
    }

    public function failed(\Throwable $exception): void
    {
        Log::warning('registration verification mail job failed', [
            'token_prefix' => substr($this->token, 0, 12),
            'message' => $exception->getMessage(),
            'class' => get_class($exception),
        ]);
    }
}
