<?php

namespace App\Notifications\Auth;

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

class CredentialsGenerated extends Notification // implements ShouldQueue // Implement ShouldQueue if desired
{
    // use Queueable;

    /**
     * The plain-text password.
     *
     * @var string
     */
    public string $plainPassword;

    /**
     * Create a new notification instance.
     *
     * @param string $plainPassword
     * @return void
     */
    public function __construct(string $plainPassword)
    {
        $this->plainPassword = $plainPassword;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable The profile model (User, Org, etc.)
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail']; // Send via email
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable The profile model
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        // TODO: make better email. Note that $notifiable is the new created user/org/bank/admin model.
        // only model properties from db are available here!

        // --- Derive the profile type directly from the model class ---
        try {
            $reflection = new \ReflectionClass($notifiable);
            $profileType = $reflection->getShortName(); // Gets 'User', 'Bank', etc.
        } catch (\ReflectionException $e) {
            // Fallback in case reflection fails
            $profileType = __('Profile');
            \Illuminate\Support\Facades\Log::error('Reflection failed in CredentialsGenerated notification: ' . $e->getMessage());
        }

        $profileName = $notifiable->full_name ?? $notifiable->name;
        $loginUrl = route('login'); 

        return (new MailMessage())
                    ->subject(__('Your :profileType profile credentials', ['profileType' => $profileType]))
                    ->greeting(__('Hello :name,', ['name' => $profileName]))
                    ->line(__('A :profileType profile has been created for you by our administrator.', ['profileType' => $profileType]))
                    ->line(__('Your temporary password is: :password', ['password' => $this->plainPassword]))
                    ->line(__('Please keep this password secure.'))
                    ->action(__('Login Now'), $loginUrl)
                    ->line(__('We highly recommend changing your password after logging in for the first time.'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            // Data for database or other channels if needed
        ];
    }
}
