<?php

namespace MintyPHP\Service\Settings;

use MintyPHP\Repository\Access\RoleRepositoryInterface;
use MintyPHP\Repository\Org\DepartmentRepositoryInterface;
use MintyPHP\Repository\Settings\SettingRepositoryInterface;
use MintyPHP\Repository\Tenant\TenantRepositoryInterface;

class SettingServicesFactory
{
    private ?ThemeConfigGateway $themeConfigService = null;
    private ?SettingCacheService $settingCacheService = null;
    private ?SettingsCryptoGatewayInterface $settingsCryptoGateway = null;
    private ?SettingsMetadataGateway $settingsMetadataGateway = null;
    private ?SettingsDefaultsGateway $settingsDefaultsGateway = null;
    private ?SettingsAppGateway $settingsAppGateway = null;
    private ?SettingsApiPolicyGateway $settingsApiPolicyGateway = null;
    private ?SettingsUserLifecycleGateway $settingsUserLifecycleGateway = null;
    private ?SettingsSystemAuditGateway $settingsSystemAuditGateway = null;
    private ?SettingsFrontendTelemetryGateway $settingsFrontendTelemetryGateway = null;
    private ?SettingsMicrosoftGateway $settingsMicrosoftGateway = null;
    private ?SettingsSessionGateway $settingsSessionGateway = null;
    private ?SettingsSmtpGateway $settingsSmtpGateway = null;
    private ?SettingsLoginPersistenceGateway $settingsLoginPersistenceGateway = null;

    public function __construct(
        private readonly SettingRepositoryFactory $settingRepositoryFactory
    ) {
    }

    public function createSettingRepository(): SettingRepositoryInterface
    {
        return $this->settingRepositoryFactory->createSettingRepository();
    }

    public function createSettingsMetadataGateway(): SettingsMetadataGateway
    {
        return $this->settingsMetadataGateway ??= new SettingsMetadataGateway($this->createSettingRepository());
    }

    public function createSettingsDefaultsGateway(): SettingsDefaultsGateway
    {
        return $this->settingsDefaultsGateway ??= new SettingsDefaultsGateway(
            $this->createSettingsMetadataGateway(),
            $this->createTenantRepository(),
            $this->createRoleRepository(),
            $this->createDepartmentRepository(),
        );
    }

    public function createSettingsAppGateway(): SettingsAppGateway
    {
        return $this->settingsAppGateway ??= new SettingsAppGateway(
            $this->createSettingsMetadataGateway(),
            $this->createThemeConfigGateway(),
        );
    }

    public function createSettingsApiPolicyGateway(): SettingsApiPolicyGateway
    {
        return $this->settingsApiPolicyGateway ??= new SettingsApiPolicyGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingsUserLifecycleGateway(): SettingsUserLifecycleGateway
    {
        return $this->settingsUserLifecycleGateway ??= new SettingsUserLifecycleGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingsSystemAuditGateway(): SettingsSystemAuditGateway
    {
        return $this->settingsSystemAuditGateway ??= new SettingsSystemAuditGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingsFrontendTelemetryGateway(): SettingsFrontendTelemetryGateway
    {
        return $this->settingsFrontendTelemetryGateway ??= new SettingsFrontendTelemetryGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingsMicrosoftGateway(): SettingsMicrosoftGateway
    {
        return $this->settingsMicrosoftGateway ??= new SettingsMicrosoftGateway(
            $this->createSettingsMetadataGateway(),
            $this->createSettingsCryptoGateway(),
        );
    }

    public function createSettingsSessionGateway(): SettingsSessionGateway
    {
        return $this->settingsSessionGateway ??= new SettingsSessionGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingsSmtpGateway(): SettingsSmtpGateway
    {
        return $this->settingsSmtpGateway ??= new SettingsSmtpGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingsLoginPersistenceGateway(): SettingsLoginPersistenceGateway
    {
        return $this->settingsLoginPersistenceGateway ??= new SettingsLoginPersistenceGateway($this->createSettingsMetadataGateway());
    }

    public function createSettingCacheService(): SettingCacheService
    {
        return $this->settingCacheService ??= new SettingCacheService();
    }

    public function createThemeConfigGateway(): ThemeConfigGateway
    {
        return $this->themeConfigService ??= new ThemeConfigGateway();
    }

    public function createSettingsCryptoGateway(): SettingsCryptoGatewayInterface
    {
        return $this->settingsCryptoGateway ??= new SettingsCryptoGateway();
    }

    private function createTenantRepository(): TenantRepositoryInterface
    {
        return $this->settingRepositoryFactory->createTenantRepository();
    }

    private function createRoleRepository(): RoleRepositoryInterface
    {
        return $this->settingRepositoryFactory->createRoleRepository();
    }

    private function createDepartmentRepository(): DepartmentRepositoryInterface
    {
        return $this->settingRepositoryFactory->createDepartmentRepository();
    }
}
