<?php

namespace App\Listeners\User;

use App\Notifications\User\WelcomeNewUserNotification;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Events\Verified;

class UserEventSubscriber
{

    /**
     * Fires when a user registered.
     *
     * @param $event
     */
    public function onUserRegister($event)
    {
        if (setting('email_verification', 'off') === 'off') {
            $event->user->notify(new WelcomeNewUserNotification());
        } elseif (request()->has('welcome_email')) {
            $event->user->notify(new WelcomeNewUserNotification());
        }
    }

    public function onUserLogin($event)
    {
        $event->user->last_login = now();
        $event->user->save();
    }

    public function onUserVerified($event)
    {
        if ($event->user->hasVerifiedEmail()) {
            $event->user->notify(new WelcomeNewUserNotification());
        }
    }


    public function subscribe($events)
    {
        return [
            Login::class => 'onUserLogin',
            Registered::class => 'onUserRegister',
            Verified::class => 'onUserVerified',
        ];
    }
}
