<?php

namespace MintyPHP\App\Container\Registrars;

use MintyPHP\App\AppContainer;
use MintyPHP\App\Container\ContainerRegistrar;
use MintyPHP\App\Module\ModuleClassResolver;
use MintyPHP\App\Module\ModuleEventDispatcher;
use MintyPHP\App\Module\ModuleRegistry;
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\AssignableRoleService;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Audit\AuditRecorderInterface;
use MintyPHP\Service\Audit\ImportAuditInterface;
use MintyPHP\Service\Audit\UserLifecycleAuditInterface;
use MintyPHP\Service\Auth\AuthGatewayFactory;
use MintyPHP\Service\Auth\AuthRepositoryFactory;
use MintyPHP\Service\Auth\AuthServicesFactory;
use MintyPHP\Service\Branding\BrandingServicesFactory;
use MintyPHP\Service\CustomField\CustomFieldRepositoryFactory;
use MintyPHP\Service\CustomField\CustomFieldServicesFactory;
use MintyPHP\Service\Directory\DirectoryGatewayFactory;
use MintyPHP\Service\Directory\DirectoryRepositoryFactory;
use MintyPHP\Service\Directory\DirectoryServicesFactory;
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)
        ));
        // Audit interfaces are NOT registered here — they are set AFTER module registrars
        // in registerContainer.php so the audit module can provide real implementations.
        // If the audit module is disabled, null implementations are used as fallback.
        $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),
            $c->get(CustomFieldRepositoryFactory::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(ImportAuditInterface::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(AuditRecorderInterface::class),
            $c->get(DatabaseSessionRepository::class),
            $c->get(SchedulerRepositoryFactory::class),
            $c
        ));
        $container->set(DirectoryServicesFactory::class, static fn (AppContainer $c): DirectoryServicesFactory => new DirectoryServicesFactory(
            $c->get(UserServicesFactory::class),
            $c->get(AuditRecorderInterface::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(AuditRecorderInterface::class),
            $c->get(SessionStoreInterface::class)
        ));
        $container->set(AccessPolicyFactory::class, static fn (AppContainer $c): AccessPolicyFactory => new AccessPolicyFactory(
            $c->get(PermissionService::class),
            $c->get(TenantScopeService::class),
            $c->has(ModuleRegistry::class) ? $c->get(ModuleRegistry::class) : null,
            static fn (string $class): ?object => ModuleClassResolver::resolve($c, $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(AuditRecorderInterface::class),
            $c->get(UserLifecycleAuditInterface::class),
            $c->get(UserRepositoryFactory::class),
            $c->get(UserGatewayFactory::class),
            $c->get(DatabaseSessionRepository::class),
            // Wrapped in a closure to break circular dependency:
            // UserServicesFactory -> AssignableRoleService -> RoleRepository -> DirectoryServicesFactory -> UserServicesFactory
            static fn (): AssignableRoleService => $c->get(AssignableRoleService::class),
            $c->has(ModuleEventDispatcher::class) ? $c->get(ModuleEventDispatcher::class) : null
        ));
        $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(AuditRecorderInterface::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),
            $c
        ));
    }
}
