<?php

namespace MintyPHP\Service\Auth;

use MintyPHP\Service\Access\AccessServicesFactory;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Settings\SettingsApiPolicyGateway;
use MintyPHP\Service\Settings\SettingsAppGateway;
use MintyPHP\Service\Settings\SettingsDefaultsGateway;
use MintyPHP\Service\Settings\SettingServicesFactory;
use MintyPHP\Service\Settings\SettingsLoginPersistenceGateway;
use MintyPHP\Service\Settings\SettingsMicrosoftGateway;
use MintyPHP\Service\Tenant\TenantScopeService;

class AuthGatewayFactory
{
    private ?SettingsMicrosoftGateway $settingsMicrosoftGateway = null;
    private ?SettingsApiPolicyGateway $settingsApiPolicyGateway = null;
    private ?SettingsDefaultsGateway $settingsDefaultsGateway = null;
    private ?SettingsAppGateway $settingsAppGateway = null;
    private ?SettingsLoginPersistenceGateway $settingsLoginPersistenceGateway = null;
    private ?PermissionService $permissionService = null;
    private ?AuthSettingsGateway $authSettingsGateway = null;
    private ?AuthTenantGateway $authTenantGateway = null;
    private ?AuthCryptoGateway $authCryptoGateway = null;
    private ?AuthExternalIdentityGateway $authExternalIdentityGateway = null;

    public function __construct(
        private readonly AuthRepositoryFactory $authRepositoryFactory,
        private readonly AccessServicesFactory $accessServicesFactory,
        private readonly SettingServicesFactory $settingServicesFactory,
        private readonly TenantScopeService $tenantScopeService
    ) {
    }

    public function createAuthSettingsGateway(): AuthSettingsGateway
    {
        return $this->authSettingsGateway ??= new AuthSettingsGateway(
            $this->createSettingsMicrosoftGateway(),
            $this->createSettingsApiPolicyGateway(),
            $this->createSettingsDefaultsGateway(),
            $this->createSettingsAppGateway(),
            $this->createSettingsLoginPersistenceGateway()
        );
    }

    public function getTenantScopeService(): TenantScopeService
    {
        return $this->tenantScopeService;
    }

    public function createPermissionService(): PermissionService
    {
        return $this->permissionService ??= $this->accessServicesFactory->createPermissionService();
    }

    public function createAuthTenantGateway(): AuthTenantGateway
    {
        return $this->authTenantGateway ??= new AuthTenantGateway(
            $this->authRepositoryFactory->createTenantRepository()
        );
    }

    public function createAuthCryptoGateway(): AuthCryptoGateway
    {
        return $this->authCryptoGateway ??= new AuthCryptoGateway();
    }

    public function createAuthExternalIdentityGateway(): AuthExternalIdentityGateway
    {
        return $this->authExternalIdentityGateway ??= new AuthExternalIdentityGateway(
            $this->authRepositoryFactory->createUserExternalIdentityRepository()
        );
    }

    private function createSettingsMicrosoftGateway(): SettingsMicrosoftGateway
    {
        return $this->settingsMicrosoftGateway ??= $this->settingServicesFactory->createSettingsMicrosoftGateway();
    }

    private function createSettingsApiPolicyGateway(): SettingsApiPolicyGateway
    {
        return $this->settingsApiPolicyGateway ??= $this->settingServicesFactory->createSettingsApiPolicyGateway();
    }

    private function createSettingsDefaultsGateway(): SettingsDefaultsGateway
    {
        return $this->settingsDefaultsGateway ??= $this->settingServicesFactory->createSettingsDefaultsGateway();
    }

    private function createSettingsAppGateway(): SettingsAppGateway
    {
        return $this->settingsAppGateway ??= $this->settingServicesFactory->createSettingsAppGateway();
    }

    private function createSettingsLoginPersistenceGateway(): SettingsLoginPersistenceGateway
    {
        return $this->settingsLoginPersistenceGateway ??= $this->settingServicesFactory->createSettingsLoginPersistenceGateway();
    }
}
