services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Netresearch\RteCKEditorImage\:
    resource: '../Classes/*'

  # Backend preview renderer with broken image detection (#636)
  Netresearch\RteCKEditorImage\Backend\Preview\RteImagePreviewRenderer:
    public: true
    arguments:
      $validator: '@Netresearch\RteCKEditorImage\Service\RteImageReferenceValidator'

  # Explicitly configure controllers with dependencies
  Netresearch\RteCKEditorImage\Controller\SelectImageController:
    public: true
    tags: ['backend.controller']
    arguments:
      $uriBuilder: '@TYPO3\CMS\Backend\Routing\UriBuilder'
      $resourceFactory: '@TYPO3\CMS\Core\Resource\ResourceFactory'
      $uploadFolderResolver: '@TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver'
      $elementBrowserRegistry: '@TYPO3\CMS\Backend\ElementBrowser\ElementBrowserRegistry'
      $richtext: '@TYPO3\CMS\Core\Configuration\Richtext'

  # TypoScript adapter for frontend image rendering
  # Bridges TypoScript preUserFunc to modern service architecture
  Netresearch\RteCKEditorImage\Controller\ImageRenderingAdapter:
    public: true
    arguments:
      $resolverService: '@Netresearch\RteCKEditorImage\Service\ImageResolverService'
      $renderingService: '@Netresearch\RteCKEditorImage\Service\ImageRenderingService'
      $attributeParser: '@Netresearch\RteCKEditorImage\Service\ImageAttributeParser'

  # Service architecture for frontend image rendering
  # These services implement the modern TYPO3 v13 ViewFactoryInterface pattern
  # Services are public to allow testing framework access via $this->get()
  Netresearch\RteCKEditorImage\Service\ImageAttributeParser:
    public: true
    # Pure HTML parsing - no dependencies needed

  Netresearch\RteCKEditorImage\Service\ImageResolverService:
    public: true
    arguments:
      $resourceFactory: '@TYPO3\CMS\Core\Resource\ResourceFactory'
      $processedFilesHandler: '@Netresearch\RteCKEditorImage\Utils\ProcessedFilesHandler'
      $svgSanitizer: '@TYPO3\CMS\Core\Resource\Security\SvgSanitizer'
      $logManager: '@TYPO3\CMS\Core\Log\LogManager'

  Netresearch\RteCKEditorImage\Service\ImageRenderingService:
    public: true
    arguments:
      $viewFactory: '@TYPO3\CMS\Core\View\ViewFactoryInterface'

  # Utility classes with dependencies
  Netresearch\RteCKEditorImage\Utils\ProcessedFilesHandler:
    arguments:
      $imageService: '@TYPO3\CMS\Extbase\Service\ImageService'

  Netresearch\RteCKEditorImage\DataHandling\SoftReference\RteImageSoftReferenceParser:
    public: true
    tags:
      - name: softreference.parser
        parserKey: rtehtmlarea_images

  # =========================================================================
  # Backend Image Processing Services (RTE → Database)
  # =========================================================================

  # Environment abstraction for testability (wraps TYPO3 statics)
  Netresearch\RteCKEditorImage\Service\Environment\EnvironmentInfoInterface:
    alias: Netresearch\RteCKEditorImage\Service\Environment\Typo3EnvironmentInfo

  Netresearch\RteCKEditorImage\Service\Environment\Typo3EnvironmentInfo:
    public: true

  # Security validation service (SSRF, MIME, path traversal)
  Netresearch\RteCKEditorImage\Service\Security\SecurityValidatorInterface:
    alias: Netresearch\RteCKEditorImage\Service\Security\SecurityValidator

  Netresearch\RteCKEditorImage\Service\Security\SecurityValidator:
    public: true

  # TYPO3 Core HtmlParser (not registered by TYPO3 by default)
  TYPO3\CMS\Core\Html\HtmlParser:
    public: false

  # Interface aliases for dependency injection and testing
  Netresearch\RteCKEditorImage\Service\Parser\ImageTagParserInterface:
    alias: Netresearch\RteCKEditorImage\Service\Parser\ImageTagParser

  Netresearch\RteCKEditorImage\Service\Builder\ImageTagBuilderInterface:
    alias: Netresearch\RteCKEditorImage\Service\Builder\ImageTagBuilder

  Netresearch\RteCKEditorImage\Service\Fetcher\ExternalImageFetcherInterface:
    alias: Netresearch\RteCKEditorImage\Service\Fetcher\ExternalImageFetcher

  Netresearch\RteCKEditorImage\Service\Resolver\ImageFileResolverInterface:
    alias: Netresearch\RteCKEditorImage\Service\Resolver\ImageFileResolver

  # HTML parsing for img tags
  Netresearch\RteCKEditorImage\Service\Parser\ImageTagParser:
    public: true
    arguments:
      $htmlParser: '@TYPO3\CMS\Core\Html\HtmlParser'

  # Img tag reconstruction
  Netresearch\RteCKEditorImage\Service\Builder\ImageTagBuilder:
    public: true

  # External image fetching with security
  Netresearch\RteCKEditorImage\Service\Fetcher\ExternalImageFetcher:
    public: true
    arguments:
      $securityValidator: '@Netresearch\RteCKEditorImage\Service\Security\SecurityValidatorInterface'
      $requestFactory: '@TYPO3\CMS\Core\Http\RequestFactory'

  # File resolution and processing
  Netresearch\RteCKEditorImage\Service\Resolver\ImageFileResolver:
    public: true
    arguments:
      $resourceFactory: '@TYPO3\CMS\Core\Resource\ResourceFactory'
      $securityValidator: '@Netresearch\RteCKEditorImage\Service\Security\SecurityValidatorInterface'
      $externalImageFetcher: '@Netresearch\RteCKEditorImage\Service\Fetcher\ExternalImageFetcher'
      $environmentInfo: '@Netresearch\RteCKEditorImage\Service\Environment\EnvironmentInfoInterface'

  # Main image processor orchestrator
  Netresearch\RteCKEditorImage\Service\Processor\RteImageProcessorInterface:
    alias: Netresearch\RteCKEditorImage\Service\Processor\RteImageProcessor

  Netresearch\RteCKEditorImage\Service\Processor\RteImageProcessor:
    public: true
    # Autowire disabled: RteImageProcessor requires runtime configuration (fetchExternalImages)
    # that must be read from extension settings via the factory
    autowire: false
    factory: ['@Netresearch\RteCKEditorImage\Service\Processor\RteImageProcessorFactory', 'create']

  # Factory for RteImageProcessor (reads extension configuration)
  Netresearch\RteCKEditorImage\Service\Processor\RteImageProcessorFactory:
    public: true
    arguments:
      $parser: '@Netresearch\RteCKEditorImage\Service\Parser\ImageTagParser'
      $builder: '@Netresearch\RteCKEditorImage\Service\Builder\ImageTagBuilder'
      $fileResolver: '@Netresearch\RteCKEditorImage\Service\Resolver\ImageFileResolver'
      $externalFetcher: '@Netresearch\RteCKEditorImage\Service\Fetcher\ExternalImageFetcher'
      $environmentInfo: '@Netresearch\RteCKEditorImage\Service\Environment\EnvironmentInfoInterface'
      $securityValidator: '@Netresearch\RteCKEditorImage\Service\Security\SecurityValidatorInterface'
      $context: '@TYPO3\CMS\Core\Context\Context'
      $uploadFolderResolver: '@TYPO3\CMS\Core\Resource\DefaultUploadFolderResolver'
      $extensionConfiguration: '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration'

  # DataHandler hook using new service architecture
  Netresearch\RteCKEditorImage\Database\RteImagesDbHook:
    public: true
    arguments:
      $imageProcessor: '@Netresearch\RteCKEditorImage\Service\Processor\RteImageProcessorInterface'

  # =========================================================================
  # Image Reference Validation (CLI command + Upgrade wizard)
  # =========================================================================

  Netresearch\RteCKEditorImage\Service\RteImageReferenceValidator:
    public: true
    arguments:
      $connectionPool: '@TYPO3\CMS\Core\Database\ConnectionPool'
      $resourceFactory: '@TYPO3\CMS\Core\Resource\ResourceFactory'
      $htmlParser: '@TYPO3\CMS\Core\Html\HtmlParser'

  # CLI: bin/typo3 rte_ckeditor_image:validate [--fix] [--table=tt_content]
  # Auto-registered via #[AsCommand] attribute
  Netresearch\RteCKEditorImage\Command\ValidateImageReferencesCommand:
    arguments:
      $validator: '@Netresearch\RteCKEditorImage\Service\RteImageReferenceValidator'
    tags:
      - name: console.command
        command: 'rte_ckeditor_image:validate'

  # Upgrade wizard: auto-discovered via #[UpgradeWizard] attribute
  Netresearch\RteCKEditorImage\Updates\ValidateRteImageReferencesWizard:
    arguments:
      $validator: '@Netresearch\RteCKEditorImage\Service\RteImageReferenceValidator'

  # Event listener: update stale src attributes when FAL files move/rename (#612)
  Netresearch\RteCKEditorImage\Listener\FileOperation\UpdateImageReferences:
    arguments:
      $connectionPool: '@TYPO3\CMS\Core\Database\ConnectionPool'
      $htmlParser: '@TYPO3\CMS\Core\Html\HtmlParser'
      $logManager: '@TYPO3\CMS\Core\Log\LogManager'
    tags:
      - name: event.listener
        identifier: 'nr-rte-update-image-references-moved'
        event: TYPO3\CMS\Core\Resource\Event\AfterFileMovedEvent
        method: handleFileMoved
      - name: event.listener
        identifier: 'nr-rte-update-image-references-renamed'
        event: TYPO3\CMS\Core\Resource\Event\AfterFileRenamedEvent
        method: handleFileRenamed

  # Event listener for automatic RTE softref enforcement
  Netresearch\RteCKEditorImage\Listener\TCA\RteSoftrefEnforcer:
    arguments:
      $extensionConfiguration: '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration'
    tags:
      - name: event.listener
        identifier: 'nr-rte-softref-enforcer'
        event: TYPO3\CMS\Core\Configuration\Event\AfterTcaCompilationEvent
        after: '*'

  # Event listener for automatic preview renderer registration (#636)
  Netresearch\RteCKEditorImage\Listener\TCA\RtePreviewRendererRegistrar:
    arguments:
      $extensionConfiguration: '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration'
    tags:
      - name: event.listener
        identifier: 'nr-rte-preview-renderer-registrar'
        event: TYPO3\CMS\Core\Configuration\Event\AfterTcaCompilationEvent
        after: '*'
