<?php

namespace App\Auth;

use App\Models\GameAccountUser;
use App\Services\AzerothCoreAccountService;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class AzerothCoreUserProvider implements UserProvider
{
    public function __construct(private readonly AzerothCoreAccountService $accounts)
    {
    }

    public function retrieveById($identifier): ?Authenticatable
    {
        if (! is_numeric($identifier)) {
            return null;
        }

        return $this->accounts->findUserById((int) $identifier);
    }

    public function retrieveByToken($identifier, #[\SensitiveParameter] $token): ?Authenticatable
    {
        return null;
    }

    public function updateRememberToken(Authenticatable $user, #[\SensitiveParameter] $token): void
    {
    }

    public function retrieveByCredentials(#[\SensitiveParameter] array $credentials): ?Authenticatable
    {
        $username = $credentials['username'] ?? null;

        if (! is_string($username) || trim($username) === '') {
            return null;
        }

        return $this->accounts->findUserByUsername($username);
    }

    public function validateCredentials(Authenticatable $user, #[\SensitiveParameter] array $credentials): bool
    {
        return $user instanceof GameAccountUser
            && isset($credentials['password'])
            && is_string($credentials['password'])
            && $this->accounts->validatePassword($user, $credentials['password']);
    }

    public function rehashPasswordIfRequired(Authenticatable $user, #[\SensitiveParameter] array $credentials, bool $force = false): void
    {
    }
}
