<?php

declare(strict_types=1);

namespace App\Mail;

use App\Helpers\SessionHelper;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class TwoFactorOtp extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public string $otp,
        public ?string $userName = null,
        public ?string $ipAddress = null,
        public ?string $userAgent = null,
        public int $expiresInMinutes = 10,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: __('Verification Code for your :app Account (:code)', [
                'code' => $this->otp,
                'app' => config('app.name'),
            ]),
        );
    }

    public function content(): Content
    {
        $client = SessionHelper::parseUserAgent($this->userAgent);

        return new Content(
            markdown: 'emails.two-factor-otp',
            with: [
                'otp' => $this->otp,
                'userName' => $this->userName,
                'expiresInMinutes' => $this->expiresInMinutes,
                'ipAddress' => $this->ipAddress,
                'browser' => $client['browser'],
                'os' => $client['os'],
                'requestedAt' => now()->format('d M Y, H:i T'),
            ],
        );
    }
}
