# This file is the entry point to configure your own services and load the default configuration.
# If you want to remove a service, deprecate it, or change its class, you can apply the changes here.
#
# If you are *not* using the default services.yaml file in your package, you *must* pass an
# empty array to the $configs argument of the Kernel::buildContainer() method.
#
# If you *are* using the default services.yaml file, then add your service configuration here.
# This may be the case if you've added a new service and are getting a "The service has been removed or made private" error.
#
# For more information, see https://symfony.com/doc/current/service_container.html#explicitly-configuring-services-and-arguments

parameters:
    # OAuth2-Server (League): RSA unter diesen Pfaden; Defuse-Key aus OAUTH2_ENCRYPTION_KEY (ASCII)
    env(OAUTH2_PRIVATE_KEY_PATH): '%kernel.project_dir%/var/oauth2/private.key'
    env(OAUTH2_PUBLIC_KEY_PATH): '%kernel.project_dir%/var/oauth2/public.key'
    env(OAUTH2_PRIVATE_KEY_PASSPHRASE): ''
    env(OAUTH2_ENCRYPTION_KEY): ''
    env(TERRAFLOW_NO_LICENSE_NEEDED): '0'
    # Basis-URL der Lizenz-API (ohne Anzeige im Admin; nur serverseitig)
    env(TERRAFLOW_LICENSE_API_URL): 'https://itsandre.mov/api/license'
    env(TERRAFLOW_LICENSE_SERVER_API_KEY): ''
    # Symfony HttpCache vor dem Kernel (nur wenn public/index.php HttpCache wrappt + HTTP_CACHE=1)
    env(HTTP_CACHE): '0'
    media_directory: '%kernel.project_dir%/public/uploads/media'
    favicons_directory: '%kernel.project_dir%/public/uploads/favicons'
    maintenance_custom_template_path: '%kernel.project_dir%/custom/template/page/maintenance/index.html.twig'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event listeners, etc.

    _instanceof:
        TerraFlow\ScheduledTask\ScheduledTaskHandlerInterface:
            tags: ['app.scheduled_task']
        TerraFlow\Admin\AdminBreadcrumbProviderInterface:
            tags: ['admin.breadcrumb_provider']
        TerraFlow\Mail\EmailTemplateTypeProviderInterface:
            tags: ['app.email_template_type_provider']
        TerraFlow\Privacy\CookieCategoryProviderInterface:
            tags: ['terraflow.cookie_category_provider']
        TerraFlow\Privacy\GdprDataExporterInterface:
            tags: ['terraflow.gdpr_data_exporter']
        TerraFlow\Privacy\GdprUserDataEraserInterface:
            tags: ['terraflow.gdpr_user_data_eraser']

    TerraFlow\ScheduledTask\ScheduledTaskRegistry:
        arguments:
            $handlers: !tagged_iterator
                tag: 'app.scheduled_task'
                index_by: 'getName'

    TerraFlow\Mail\EmailTemplateTypeRegistry:
        arguments:
            $providers: !tagged_iterator
                tag: 'app.email_template_type_provider'

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    TerraFlow\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Mail/EmailTemplateTypeRegistry.php'
            - '../src/ScheduledTask/ScheduledTaskRegistry.php'
            - '../src/Privacy/CookieCategoryRegistry.php'
            - '../src/Privacy/GdprExportService.php'
            - '../src/Privacy/GdprErasureService.php'

    TerraFlow\Privacy\CookieCategoryRegistry:
        arguments:
            $providers: !tagged_iterator
                tag: 'terraflow.cookie_category_provider'

    TerraFlow\Privacy\GdprExportService:
        arguments:
            $exporters: !tagged_iterator
                tag: 'terraflow.gdpr_data_exporter'

    TerraFlow\Privacy\GdprErasureService:
        arguments:
            $erasers: !tagged_iterator
                tag: 'terraflow.gdpr_user_data_eraser'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

    # Container Interface Alias
    Symfony\Component\DependencyInjection\ContainerInterface: '@service_container'

    # Event Dispatcher Service
    TerraFlow\Service\EventDispatcherService:
        arguments:
            $eventDispatcher: '@event_dispatcher'
            $logger: '@logger'
            $security: '@security.helper'
            $loggingService: '@TerraFlow\Service\LoggingService'

    # Logging Service (Monolog-Kanal „terraflow“ → siehe config/packages/monolog.yaml)
    TerraFlow\Service\LoggingService:
        arguments:
            $logger: '@monolog.logger.terraflow'
            $security: '@security.helper'
            $filesystem: '@filesystem'
            $projectDir: '%kernel.project_dir%'

    TerraFlow\Service\License\LicenseApiRequestGuard:
        arguments:
            $apiKey: '%env(TERRAFLOW_LICENSE_SERVER_API_KEY)%'

    TerraFlow\Service\LicenseService:
        arguments:
            $cache: '@cache.app'
        bind:
            $licenseApiBaseUrl: '%env(TERRAFLOW_LICENSE_API_URL)%'
            $licenseServerApiKey: '%env(TERRAFLOW_LICENSE_SERVER_API_KEY)%'

    TerraFlow\EventSubscriber\LicenseCheckSubscriber:
        arguments:
            $cache: '@cache.app'

    TerraFlow\Command\SeedLicensePresetsCommand:
        bind:
            $projectDir: '%kernel.project_dir%'

    TerraFlow\Service\License\LicenseSaaSPricingGlobalsLoader:
        arguments:
            $pricingDisplayYamlPath: '%kernel.project_dir%/plugin/LicenseServerPlugin/config/pricing-display.yaml'

    TerraFlow\Service\License\LicenseStorefrontSignupService:
        arguments:
            $invoicePaymentTermsDays: 14

    TerraFlow\Monolog\RequestContextProcessor:
        autoconfigure: false
        autowire: true
        tags:
            - { name: monolog.processor, handler: main }

    # Logging Interface Alias
    TerraFlow\Interface\LoggingInterface: '@TerraFlow\Service\LoggingService'

    # Twig Extensions
    TerraFlow\Twig\PermissionExtension:
        arguments:
            $permissionService: '@TerraFlow\Service\PermissionService'
        tags:
            - { name: twig.extension }
    
    TerraFlow\Twig\RouteExtension:
        arguments:
            $routeService: '@TerraFlow\Service\RouteService'
        tags:
            - { name: twig.extension }
    
    TerraFlow\Twig\CustomFieldExtension:
        arguments:
            $customFieldService: '@TerraFlow\Service\CustomFieldService'
        tags:
            - { name: twig.extension }

    TerraFlow\Service\AdminBreadcrumbResolver:
        arguments:
            $routeMap: '%admin_breadcrumb.routes%'
            $providers: !tagged_iterator admin.breadcrumb_provider

    TerraFlow\Twig\AdminBreadcrumbExtension:
        tags:
            - { name: twig.extension }

    # Environment Override Service
    TerraFlow\Service\EnvironmentOverrideService:
        arguments:
            $licenseService: '@TerraFlow\Service\LicenseService'
            $cache: '@cache.app'
            $params: '@parameter_bag'

    # Log Cleanup Service
    TerraFlow\Service\LogCleanupService:
        arguments:
            $loggingService: '@TerraFlow\Service\LoggingService'
            $logger: '@logger'

    TerraFlow\Service\Admin\MonologFileViewerService:
        arguments:
            $projectDir: '%kernel.project_dir%'

    TerraFlow\Controller\StorefrontController:
        arguments:
            $storefrontPageHttpCache: '%env(bool:STOREFRONT_PAGE_HTTP_CACHE)%'

    # TOTP-Formular: gleiche Theme-Auflösung wie login/register (Standard- vs. aktives Plugin-Theme)
    scheb_two_factor.security.totp.default_form_renderer:
        class: TerraFlow\Security\TwoFactor\StorefrontTwoFactorFormRenderer
        arguments:
            $twigEnvironment: '@twig'
            $templateResolver: '@TerraFlow\Service\StorefrontThemeTemplateResolver'

    TerraFlow\Service\RecoveryCodeAuthenticationService:
        arguments:
            $recovery2faLoginLimiter: '@limiter.recovery_2fa_login'

    TerraFlow\Security\TwoFactor\RecoveryTwoFactorProvider:
        arguments:
            $formRenderer: '@TerraFlow\Security\TwoFactor\StorefrontRecoveryTwoFactorFormRenderer'
        tags:
            - { name: scheb_two_factor.provider, alias: recovery }

    TerraFlow\Security\TwoFactor\StorefrontRecoveryTwoFactorFormRenderer:
        arguments:
            $twigEnvironment: '@twig'
            $templateResolver: '@TerraFlow\Service\StorefrontThemeTemplateResolver'

    Jbtronics\TFAWebauthn\Services\WebauthnProvider:
        alias: jbtronics_webauthn_tfa.webauthn_provider

    jbtronics_webauthn_tfa.webauthn_authenticator:
        class: TerraFlow\Security\WebauthnAuthenticatorWithoutU2FAppIdExtension
        arguments:
            $publicKeyCredentialSourceRepository: '@jbtronics_webauthn_tfa.user_public_key_source_repo'
            $u2FAppIDProvider: '@jbtronics_webauthn_tfa.u2f_app_id_provider'
            $webauthnProvider: '@jbtronics_webauthn_tfa.webauthn_provider'
            $PSRRequestHelper: '@jbtronics_webauthn_tfa.psr_request_helper'
            $rpID: '%jbtronics_webauthn_tfa.rpID%'
            $timeout: '%jbtronics_webauthn_tfa.timeout%'
            $logger: '@logger'

    # User Provider
    TerraFlow\Security\UserProvider:
        arguments:
            $userRepository: '@TerraFlow\Repository\UserRepository'
            $settingsRepository: '@TerraFlow\Repository\SettingsRepository'

    # Cache Services
    terraflow.cache.filesystem:
        class: Symfony\Component\Cache\Adapter\FilesystemAdapter
        arguments:
            - 'terraflow'
            - 0
            - '%kernel.cache_dir%/pools'

    terraflow.cache.pages:
        class: Symfony\Component\Cache\Adapter\TagAwareAdapter
        arguments:
            - '@terraflow.cache.filesystem'
            - '@terraflow.cache.filesystem'

    terraflow.cache.media:
        class: Symfony\Component\Cache\Adapter\TagAwareAdapter
        arguments:
            - '@terraflow.cache.filesystem'
            - '@terraflow.cache.filesystem'

    terraflow.cache.system:
        class: Symfony\Component\Cache\Adapter\TagAwareAdapter
        arguments:
            - '@terraflow.cache.filesystem'
            - '@terraflow.cache.filesystem'

    terraflow.cache.plugins:
        class: Symfony\Component\Cache\Adapter\TagAwareAdapter
        arguments:
            - '@terraflow.cache.filesystem'
            - '@terraflow.cache.filesystem'

    # Event Listeners (automatisch durch AsEventListener-Attribute registriert)
    TerraFlow\EventListener\:
        resource: '../src/EventListener/'
    
    # Media Upload Event Subscriber
    TerraFlow\EventSubscriber\MediaUploadEventSubscriber:
        arguments:
            $mediaDirectory: '%media_directory%'

    TerraFlow\Service\Image\ImageCompressionService:
        arguments:
            $mediaDirectory: '%media_directory%'

    # Doctrine Subscriber: hydrate Custom Fields onto entities after load
    TerraFlow\EventSubscriber\CustomFieldHydratorSubscriber:
        tags:
            - { name: doctrine.event_subscriber }

    Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory: ~
    Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory: ~

    # League-OAuth2-Server: Interfaces für Oauth2ServerFactory autowiren
    League\OAuth2\Server\Repositories\ClientRepositoryInterface: '@TerraFlow\OAuth2\Repository\LeagueClientRepository'
    League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface: '@TerraFlow\OAuth2\Repository\LeagueAccessTokenRepository'
    League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface: '@TerraFlow\OAuth2\Repository\LeagueAuthCodeRepository'
    League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface: '@TerraFlow\OAuth2\Repository\LeagueRefreshTokenRepository'
    League\OAuth2\Server\Repositories\ScopeRepositoryInterface: '@TerraFlow\OAuth2\Repository\LeagueScopeRepository'

    TerraFlow\OAuth2\Oauth2ServerFactory:
        arguments:
            $privateKeyPath: '%env(resolve:OAUTH2_PRIVATE_KEY_PATH)%'
            $publicKeyPath: '%env(resolve:OAUTH2_PUBLIC_KEY_PATH)%'
            $privateKeyPassphrase: '%env(OAUTH2_PRIVATE_KEY_PASSPHRASE)%'
            $encryptionKeyAscii: '%env(OAUTH2_ENCRYPTION_KEY)%'

    League\OAuth2\Server\AuthorizationServer:
        factory: ['@TerraFlow\OAuth2\Oauth2ServerFactory', 'createAuthorizationServer']

    League\OAuth2\Server\ResourceServer:
        factory: ['@TerraFlow\OAuth2\Oauth2ServerFactory', 'createResourceServer']

    # Twig Extensions