<?php

namespace App\Modules\BaseAuth\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property string $name
 * @property string $request_method
 * @property string|null $ip
 * @property string|null $user_agent
 * @property string|null $target_url
 * @property string|null $options
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 */
class AuthEvent extends Model
{
    protected $table = 'auth_events';

    /**
     * When user registers to application
     */
    public const REGISTER = 'REGISTER';

    /**
     * When user logs into application
     */
    public const LOGIN = 'LOGIN';

    /**
     * When user verifies phone number
     */
    public const PHONE_VERIFICATION = 'PHONE_VERIFICATION';

    /**
     * When user verifies phone number
     */
    public const PHONE_CHANGED = 'PHONE_CHANGED';

    /**
     * When user logs out of application
     */
    public const LOGOUT = 'LOGOUT';

    /**
     * When user resets password
     */
    public const PASSWORD_RESET = 'PASSWORD_RESET';

    /**
     * When user resets password
     */
    public const FAILED = 'FAILED';

    /**
     * When user resets password
     */
    public const ATTEMPTING = 'ATTEMPTING';

    /**
     * When user resets password
     */
    public const LOCKOUT = 'LOCKOUT';

    /**
     * Laravel's default events
     *
     * @return array<string, string>
     */
    public static function laravelDefaultEvents(): array
    {
        return [
            'Illuminate\\Auth\\Events\\Attempting' => self::ATTEMPTING,
            'Illuminate\\Auth\\Events\\Failed' => self::FAILED,
            'Illuminate\\Auth\\Events\\Lockout' => self::LOCKOUT,
        ];
    }

    /**
     * Guest the event
     */
    public static function guessEvent(string|object $event): string
    {
        if (is_object($event)) {
            $event = get_class($event);
        }

        return self::laravelDefaultEvents()[$event] ?? '';
    }

    /**
     * Log type
     */
    public static function logType(string $name): string
    {
        return match ($name) {
            self::REGISTER => 'notice',
            self::LOGIN => 'notice',
            self::PHONE_VERIFICATION => 'info',
            self::LOGOUT => 'notice',
            self::PASSWORD_RESET => 'info',
            self::FAILED => 'warning',
            self::ATTEMPTING => 'alert',
            self::LOCKOUT => 'alert',
            default => 'info',
        };
    }
}
