<?php

namespace App\Http\Livewire;

use Livewire\Component;
use WireUi\Traits\WireUiActions;


class NotifySwitchProfile extends Component
{
    use WireUiActions;


    public function mount()
    {
        $this->notify();
    }

    public function notify()
    {
        // WireUI notification

        $this->notification()->success(
            $title = __('Profile switch'),
            $description = __('Your profile has been switched successfully')
        );

        // Check if the profile's email needs verification
        $this->checkEmailVerification();
    }

    /**
     * Check if the current profile's email needs verification and show a warning
     */
    private function checkEmailVerification()
    {
        // Don't show unverified warning if we just verified the email
        if (session('email-verified')) {
            return;
        }

        $currentProfile = getActiveProfile();

        // Check if profile implements MustVerifyEmail and has unverified email
        if ($currentProfile instanceof \Illuminate\Contracts\Auth\MustVerifyEmail && !$currentProfile->hasVerifiedEmail()) {
            $profileName = $currentProfile->name ?? __('Your profile');

            $this->notification()->warning(
                $title = __('Email Verification Required'),
                $description = __('The email address of :profile_name is unverified. Please verify it to ensure you can receive important notifications.', ['profile_name' => $profileName])
            );
        }
    }


    public function dehydrate()
    {
        // Clear the session key after the component is rendered
        session()->forget('profile-switched-notification');
    }
    
    public function render()
    {
        return view('livewire.notify-switch-profile');
    }
}
