<?php

namespace Src\Modules\Auth\Application\Admin;

use Src\Modules\Auth\Interfaces\Application\Admin\IFactory;
use Src\Modules\Auth\Interfaces\Application\IFactory as IApplicationFactory;
use Src\Modules\Auth\Interfaces\IFactory as IModuleFactory;
use Src\Modules\Auth\Interfaces\Application\Admin\IDomain;

class Factory implements IFactory
{
    protected IApplicationFactory $applicationFactory;

    private ?IDomain $domain = null;

    public function setApplicationFactory(IApplicationFactory $factory): void
    {
        $this->applicationFactory = $factory;
    }

    private function createValidator(): Validator
    {
        $validator = new Validator();
        $adminsTable = $this->applicationFactory->getModuleFactory()->getSetting(IModuleFactory::ADMINS_TABLE);
        $validator->setAdminsTable($adminsTable);
        return $validator;
    }

    public function getDomain(): Domain
    {
        if($this->domain){
            return $this->domain;
        }
        $this->domain = new Domain();
        $validator = $this->createValidator();
        $this->domain->setValidator($validator);
        $adminDtoFactory = $this->applicationFactory->getModuleFactory()->getDtoFactory()->getAdminFactory();
        $this->domain->setAdminDtoFactory($adminDtoFactory);
        $adminPersistLayer = $this->applicationFactory->getModuleFactory()->getInfrastructureFactory()->getPersistLayerFactory()->getAdminsLayer();
        $this->domain->setAdminsLayer($adminPersistLayer);
        $adminsStorage = $this->applicationFactory->getModuleFactory()->getInfrastructureFactory()->getStorageFactory()->getAdminsStorage();
        $this->domain->setAdminsStorage($adminsStorage);
        return $this->domain;
    }
}