<?php

namespace Src\Modules\Auth\Laravel;

use Src\Modules\Auth\Interfaces\Laravel\IUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Hash;
use Src\Modules\Auth\Interfaces\Laravel\IFactory;
use Src\Modules\Auth\Interfaces\Infrastructure\Storage\IAdminsStorage;
use Src\Modules\Auth\Interfaces\Infrastructure\PersistLayer\IAdminsLayer;
use Src\Modules\Auth\Interfaces\Dto\Admin\IFactory as IAdminDtoFactory;

class UserProvider implements IUserProvider
{


    protected IAdminsLayer $adminLayer;

    protected IAdminDtoFactory $adminDtoFactory;

    protected IAdminsStorage $adminsStorage;

    protected IFactory $factory;

    public function setAdminsStorage(IAdminsStorage $storage): void
    {
        $this->adminsStorage = $storage;
    }

    public function setAdminsLayer(IAdminsLayer $layer): void
    {
        $this->adminLayer = $layer;
    }

    public function setAdminDtoFactory(IAdminDtoFactory $factory): void
    {
        $this->adminDtoFactory = $factory;
    }

    public function setFactory(IFactory $factory): void
    {
        $this->factory = $factory;
    }

    public function retrieveById($identifier)
    {
        $type = explode('_', $identifier)[0];
        if ($type == 'admin') {
            $userData = $this->adminsStorage->getById($identifier);
            $user = $this->factory->createUser();
            $user->load($userData);
            return $user;
        }
        return null;
    }

    public function retrieveByToken($identifier, $token)
    {
        // $type = explode('_', $identifier)[0];
        // if($type == 'user'){
        //     $userData = $this->usersStorage->getById($identifier);
        //     if(!$userData || $userData['remember_token'] != $token){
        //         return null;
        //     }
        //     $user = $this->factory->createUser();
        //     $user->load($userData);
        //     return $user;
        // }
        // return null;
    }

    public function updateRememberToken(Authenticatable $user, $token)
    {
        /** @var \Src\Modules\Auth\Interfaces\Dto\IAbstractUser $user */
        // $userPersist = $this->userDtoFactory->createUserPersist();
        // $userPersist->load($user->getAttributes());
        // $userPersist->update(['remember_token' => $token]);
        // $this->userPersistLayer->update($userPersist);
    }

    public function retrieveByCredentials(array $credentials)
    {
        $email = $credentials['email'];
        $type = $credentials['type'];
        if ($type == 'admin') {
            $userData = $this->adminsStorage->getByEmail($email);
            $user = $this->factory->createUser();
            $user->load($userData);
            return $user;
        }
        return null;
    }

    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        $passwordHash = $user->getAuthPassword();
        $password = $credentials['password'];
        return Hash::check($password, $passwordHash);
    }

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