<?php

namespace App\Notifications;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;

class CustomVerifyEmail extends VerifyEmail
{
    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        // Store current locale in cache for this user (expires in 24 hours)
        try {
            $currentLocale = app()->getLocale();
            Cache::put('user_locale_' . $notifiable->id, $currentLocale, now()->addHours(24));
        } catch (\Exception $e) {
            // If cache fails, continue anyway - locale persistence is not critical
            \Log::warning('Failed to cache user locale', ['error' => $e->getMessage()]);
        }

        return (new MailMessage)
            ->subject(__('mail/verification.subject'))
            ->view('emails.verify-email', [
                'notifiable' => $notifiable,
                'verificationUrl' => $verificationUrl,
            ]);
    }
}
