<?php

namespace App\Notifications;

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

class NonUserPasswordResetNotification extends Notification implements ShouldQueue
{
    use Queueable;

    /**
     * The password reset URL.
     *
     * @var string
     */
    public $resetUrl;

    /**
     * The name of the profile.
     *
     * @var string
     */
    public $profileName;

    /**
     * The type of the profile (e.g., 'admin', 'bank').
     *
     * @var string
     */
    public $profileType;

    /**
     * The locale for the notification.
     *
     * @var string
     */
    public $locale;


    /**
     * Create a new notification instance.
     *
     * @param  string  $resetUrl
     * @param  object  $notifiable The profile model (Admin, Bank, etc.)
     * @return void
     */
    public function __construct(string $resetUrl, $notifiable)
    {
        $this->resetUrl = $resetUrl;
        // Assuming the notifiable (Admin, Bank model) has a 'name' or 'full_name' attribute
        $this->profileName = $notifiable->name ?? $notifiable->full_name ?? 'your account';

        // Determine profile type for more specific messaging if needed
        if ($notifiable instanceof \App\Models\Admin) {
            $this->profileType = 'Admin';
        } elseif ($notifiable instanceof \App\Models\Bank) {
            $this->profileType = 'Bank';
        } elseif ($notifiable instanceof \App\Models\Organization) {
            $this->profileType = 'Organization';
        } else {
            $this->profileType = 'Profile';
        }

        // Support lang_preference for Organization, Bank, and Admin models
        $this->locale = $notifiable->lang_preference ?? config('app.fallback_locale');

        // Ensure locale is not empty
        if (empty($this->locale)) {
            $this->locale = config('app.fallback_locale');
        }

        $this->onQueue('high');
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        // Set application locale for this email
        app()->setLocale($this->locale);

        $subject = trans('Reset Password Notification', [], $this->locale);
        $expireMinutes = config('auth.passwords.' . $this->getBroker($notifiable) . '.expire');

        return (new MailMessage())
            ->from(timebank_config('mail.support.email'), timebank_config('mail.support.name'))
            ->subject($subject)
            ->view('emails.auth.reset-password', [
                'notifiable' => $notifiable,
                'resetUrl' => $this->resetUrl,
                'subject' => $subject,
                'expireMinutes' => $expireMinutes,
                'locale' => $this->locale,
            ]);
    }

    /**
     * Get the broker for the notifiable.
     */
    protected function getBroker($notifiable)
    {
        if ($notifiable instanceof \App\Models\Admin) {
            return 'admins';
        }
        if ($notifiable instanceof \App\Models\Bank) {
            return 'banks';
        }
        if ($notifiable instanceof \App\Models\Organization) {
            return 'organizations';
        }
        return 'users'; // Default
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
