<?php

declare(strict_types=1);

namespace Drupal\native_observability_cache_observer\Service;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Inspects the runtime environment for cache-affecting debug settings.
 */
final class CacheObserverEnvironmentInspector {
  use StringTranslationTrait;

  /**
   * Creates a new environment inspector instance.
   */
  public function __construct(
    private readonly ConfigFactoryInterface $configFactory,
    private readonly CacheObserverEnvironmentSnapshotManager $snapshotManager,
  ) {
  }

  /**
   * Inspect environment.
   */
  public function inspect(): array {
    $warnings = [];

    $twigConfig = $this->configFactory->get('twig.config');
    if ((bool) $twigConfig->get('debug')) {
      $warnings[] = $this->t('Twig debug is enabled.');
    }

    return [
      'has_warnings' => !empty($warnings),
      'warnings' => $warnings,
      'summary' => $this->t('Development environment detected. Cache signals may be incomplete.'),
    ];
  }

  /**
   * Compatibility wrapper used by cache observers.
   *
   * This method delegates to the environment snapshot manager
   * so existing observers can retrieve cache backend information.
   */
  public function getCacheBackendSnapshot(): array {
    $snapshot = $this->snapshotManager->getSnapshot();

    return $snapshot['cache']['bins'] ?? [];
  }

}
