<?php

declare(strict_types=1);

namespace Drupal\native_observability_dashboard\Service;

use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandlerInterface;

/**
 * Resolves dashboard feature availability.
 */
final class DashboardAvailabilityResolver {

  /**
   * Defines the main trace storage table name.
   */
  private const TRACE_TABLE = 'native_observability_trace';

  /**
   * Creates a new availability resolver instance.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The Drupal module handler service.
   * @param \Drupal\Core\Database\Connection $database
   *   The active database connection.
   */
  public function __construct(
    private readonly ModuleHandlerInterface $moduleHandler,
    private readonly Connection $database,
  ) {
  }

  /**
   * Checks whether the trace layer is available.
   *
   * @return bool
   *   TRUE when the core module and trace table are available, FALSE otherwise.
   */
  public function hasTraceSupport(): bool {
    if (!$this->moduleHandler->moduleExists('native_observability')) {
      return FALSE;
    }

    return $this->database->schema()->tableExists(self::TRACE_TABLE);
  }

}
