<?php

namespace Src\Modules\Auth\Laravel;

use Src\Modules\Auth\Interfaces\Laravel\IFactory;
use Src\Modules\Auth\Interfaces\IFactory as IModuleFactory;
use Src\Modules\Auth\Interfaces\Laravel\IUserProvider;
use Src\Modules\Auth\Laravel\User;

class Factory implements IFactory {

    private IModuleFactory $moduleFactory;

    private ?IUserProvider $userProvider = null;

    public function setModuleFactory(IModuleFactory $factory):void
    {
        $this->moduleFactory = $factory;
    }

    public function createUser(): User
    {
        $user = new User();
        return $user;
    }

    public function getUserProvider(): UserProvider
    {
        if($this->userProvider){
            return $this->userProvider;
        }
        $this->userProvider = new UserProvider();
        $adminsStorage = $this->moduleFactory->getInfrastructureFactory()->getStorageFactory()->getAdminsStorage();
        $this->userProvider->setAdminsStorage($adminsStorage);
        $adminDtoFactory = $this->moduleFactory->getDtoFactory()->getAdminFactory();
        $this->userProvider->setAdminDtoFactory($adminDtoFactory);
        $adminsLayer = $this->moduleFactory->getInfrastructureFactory()->getPersistLayerFactory()->getAdminsLayer();
        $this->userProvider->setAdminsLayer($adminsLayer);
        $this->userProvider->setFactory($this);
        return $this->userProvider;
    }
}