<?php

declare(strict_types=1);

namespace Drupal\native_observability\Service;

use Drupal\Core\Database\Connection;
use Drupal\Core\Site\Settings;

/**
 * Collects safe runtime diagnostics (no secrets).
 *
 * This service is intentionally "safe-by-default":
 * - It does NOT read passwords, tokens, or private keys.
 * - It does NOT dump sensitive configuration.
 * - It provides a minimal runtime snapshot for a "status" page.
 *
 * The goal is not full monitoring, but a helpful health/diagnostic surface.
 */
final class DiagnosticsService {

  /**
   * Drupal database connection.
   */
  private Connection $database;

  /**
   * Constructs the storage.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Drupal database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * Collects runtime diagnostics.
   *
   * @return array<string, mixed>
   *   An associative array of safe runtime information.
   */
  public function collect(): array {
    return [
      'php_version' => PHP_VERSION,
      'drupal_root' => DRUPAL_ROOT,
      'environment' => Settings::get('environment', 'n/a'),
      'db_driver' => $this->database->driver(),
      'memory_limit' => ini_get('memory_limit') ?: 'n/a',
      'max_execution_time' => ini_get('max_execution_time') ?: 'n/a',
      'opcache_enabled' => function_exists('opcache_get_status') ? (bool) opcache_get_status(FALSE) : FALSE,
    ];
  }

}
