<?php

declare(strict_types=1);

namespace Drupal\native_observability_dashboard\Service;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Resolves optional dashboard sections from enabled submodules only.
 */
final readonly class OptionalSectionResolver {

  /**
   * Creates a new optional section resolver instance.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The Drupal module handler service.
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The Symfony service container.
   */
  public function __construct(
    private ModuleHandlerInterface $moduleHandler,
    private ContainerInterface $container,
  ) {
  }

  /**
   * Builds the optional performance map section.
   *
   * @return array
   *   The performance map section render payload.
   */
  public function isSpanBreakdownAvailable(): bool {
    if (!$this->moduleHandler->moduleExists('native_observability_spans')) {
      return FALSE;
    }

    /** @var \Drupal\Core\Database\Connection $database */
    $database = $this->container->get('database');

    return $database->schema()->tableExists('native_observability_spans');
  }

}
