<?php

namespace MintyPHP\App\Container\Registrars;

use MintyPHP\App\AppContainer;
use MintyPHP\App\Container\ContainerRegistrar;
use MintyPHP\Http\CookieStoreInterface;
use MintyPHP\Http\RequestRuntimeInterface;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Repository\Support\DatabaseSessionRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Service\Access\AccessGatewayFactory;
use MintyPHP\Service\Access\AccessPolicyFactory;
use MintyPHP\Service\Access\AccessRepositoryFactory;
use MintyPHP\Service\Access\AccessServicesFactory;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\AddressBook\AddressBookServicesFactory;
use MintyPHP\Service\Audit\AuditRepositoryFactory;
use MintyPHP\Service\Audit\AuditServicesFactory;
use MintyPHP\Service\Auth\AuthGatewayFactory;
use MintyPHP\Service\Auth\AuthRepositoryFactory;
use MintyPHP\Service\Auth\AuthServicesFactory;
use MintyPHP\Service\Branding\BrandingServicesFactory;
use MintyPHP\Service\CustomField\CustomFieldServicesFactory;
use MintyPHP\Service\Directory\DirectoryGatewayFactory;
use MintyPHP\Service\Directory\DirectoryRepositoryFactory;
use MintyPHP\Service\Directory\DirectoryServicesFactory;
use MintyPHP\Service\Import\ImportRepositoryFactory;
use MintyPHP\Service\Import\ImportServicesFactory;
use MintyPHP\Service\Mail\MailRepositoryFactory;
use MintyPHP\Service\Mail\MailServicesFactory;
use MintyPHP\Service\Scheduler\SchedulerRepositoryFactory;
use MintyPHP\Service\Scheduler\SchedulerServicesFactory;
use MintyPHP\Service\Security\SecurityRepositoryFactory;
use MintyPHP\Service\Security\SecurityServicesFactory;
use MintyPHP\Service\Settings\SettingRepositoryFactory;
use MintyPHP\Service\Settings\SettingServicesFactory;
use MintyPHP\Service\Tenant\TenantRepositoryFactory;
use MintyPHP\Service\Tenant\TenantScopeService;
use MintyPHP\Service\Tenant\TenantServicesFactory;
use MintyPHP\Service\User\UserGatewayFactory;
use MintyPHP\Service\User\UserRepositoryFactory;
use MintyPHP\Service\User\UserServicesFactory;

final class ServiceFactoryRegistrar implements ContainerRegistrar
{
    public function register(AppContainer $container): void
    {
        $container->set(SettingServicesFactory::class, static fn (AppContainer $c): SettingServicesFactory => new SettingServicesFactory(
            $c->get(SettingRepositoryFactory::class)
        ));
        $container->set(AuditServicesFactory::class, static fn (AppContainer $c): AuditServicesFactory => new AuditServicesFactory(
            $c->get(AuditRepositoryFactory::class),
            $c->get(SettingServicesFactory::class)->createSettingsSystemAuditGateway(),
            $c->get(RequestRuntimeInterface::class),
            $c->get(SessionStoreInterface::class)
        ));
        $container->set(SecurityServicesFactory::class, static fn (AppContainer $c): SecurityServicesFactory => new SecurityServicesFactory(
            $c->get(SecurityRepositoryFactory::class)
        ));
        $container->set(MailServicesFactory::class, static fn (AppContainer $c): MailServicesFactory => new MailServicesFactory(
            $c->get(MailRepositoryFactory::class),
            $c->get(SettingServicesFactory::class)->createSettingsSmtpGateway()
        ));
        $container->set(BrandingServicesFactory::class, static fn (AppContainer $c): BrandingServicesFactory => new BrandingServicesFactory(
            $c->get(SettingServicesFactory::class)->createSettingsAppGateway()
        ));
        $container->set(CustomFieldServicesFactory::class, static fn (AppContainer $c): CustomFieldServicesFactory => new CustomFieldServicesFactory(
            $c->get(TenantRepository::class),
            $c->get(TenantScopeService::class)
        ));
        $container->set(TenantServicesFactory::class, static fn (AppContainer $c): TenantServicesFactory => new TenantServicesFactory(
            $c->get(PermissionService::class),
            $c->get(TenantRepositoryFactory::class)
        ));
        $container->set(ImportServicesFactory::class, static fn (AppContainer $c): ImportServicesFactory => new ImportServicesFactory(
            $c->get(UserServicesFactory::class),
            $c->get(AccessServicesFactory::class),
            $c->get(SettingServicesFactory::class),
            $c->get(DirectoryServicesFactory::class),
            $c->get(ImportRepositoryFactory::class),
            $c->get(TenantScopeService::class),
            $c->get(SessionStoreInterface::class)
        ));
        $container->set(SchedulerServicesFactory::class, static fn (AppContainer $c): SchedulerServicesFactory => new SchedulerServicesFactory(
            $c->get(UserServicesFactory::class),
            $c->get(AuditServicesFactory::class),
            $c->get(DatabaseSessionRepository::class),
            $c->get(SchedulerRepositoryFactory::class)
        ));
        $container->set(AddressBookServicesFactory::class, static fn (AppContainer $c): AddressBookServicesFactory => new AddressBookServicesFactory(
            $c->get(UserServicesFactory::class),
            $c->get(DirectoryServicesFactory::class),
            $c->get(CustomFieldServicesFactory::class),
            $c->get(TenantScopeService::class)
        ));
        $container->set(DirectoryServicesFactory::class, static fn (AppContainer $c): DirectoryServicesFactory => new DirectoryServicesFactory(
            $c->get(UserServicesFactory::class),
            $c->get(AuditServicesFactory::class),
            $c->get(DirectoryRepositoryFactory::class),
            $c->get(DirectoryGatewayFactory::class)
        ));
        $container->set(DirectoryGatewayFactory::class, static fn (AppContainer $c): DirectoryGatewayFactory => new DirectoryGatewayFactory(
            $c->get(SettingServicesFactory::class),
            $c->get(TenantScopeService::class)
        ));
        $container->set(AccessServicesFactory::class, static fn (AppContainer $c): AccessServicesFactory => new AccessServicesFactory(
            $c->get(AccessRepositoryFactory::class),
            $c->get(AccessGatewayFactory::class),
            // Wrapped in a second closure to break the circular dependency:
            // AccessServicesFactory <-> AccessPolicyFactory via PermissionService.
            static fn (): AccessPolicyFactory => $c->get(AccessPolicyFactory::class)
        ));
        $container->set(AccessGatewayFactory::class, static fn (AppContainer $c): AccessGatewayFactory => new AccessGatewayFactory(
            $c->get(AccessRepositoryFactory::class),
            $c->get(AuditServicesFactory::class),
            $c->get(SessionStoreInterface::class)
        ));
        $container->set(AccessPolicyFactory::class, static fn (AppContainer $c): AccessPolicyFactory => new AccessPolicyFactory(
            $c->get(PermissionService::class),
            $c->get(TenantScopeService::class)
        ));
        $container->set(UserGatewayFactory::class, static fn (AppContainer $c): UserGatewayFactory => new UserGatewayFactory(
            $c->get(AccessServicesFactory::class),
            $c->get(SettingServicesFactory::class),
            $c->get(TenantScopeService::class),
            $c->get(DirectoryRepositoryFactory::class),
        ));
        $container->set(UserServicesFactory::class, static fn (AppContainer $c): UserServicesFactory => new UserServicesFactory(
            $c->get(AuditServicesFactory::class),
            $c->get(UserRepositoryFactory::class),
            $c->get(UserGatewayFactory::class),
            $c->get(DatabaseSessionRepository::class)
        ));
        $container->set(AuthGatewayFactory::class, static fn (AppContainer $c): AuthGatewayFactory => new AuthGatewayFactory(
            $c->get(AuthRepositoryFactory::class),
            $c->get(AccessServicesFactory::class),
            $c->get(SettingServicesFactory::class),
            $c->get(TenantScopeService::class)
        ));
        $container->set(AuthServicesFactory::class, static fn (AppContainer $c): AuthServicesFactory => new AuthServicesFactory(
            $c->get(UserServicesFactory::class),
            $c->get(AuditServicesFactory::class),
            $c->get(MailServicesFactory::class),
            $c->get(AuthRepositoryFactory::class),
            $c->get(AuthGatewayFactory::class),
            $c->get(DatabaseSessionRepository::class),
            $c->get(SessionStoreInterface::class),
            $c->get(CookieStoreInterface::class),
            $c->get(RequestRuntimeInterface::class)
        ));
    }
}
