<?php

declare(strict_types=1);

namespace Drupal\native_observability_database_observer\Service;

/**
 * Provides lightweight helpers for relation classification and SQL hashing.
 *
 * The goal of this helper is to keep classification rules isolated so the
 * subscriber and storage service remain focused on orchestration and
 * persistence.
 */
final class DatabaseQueryClassifier {

  /**
   * Returns the relation type for a captured row.
   *
   * A record is considered request-bound when a request identifier or a route
   * name is available. Otherwise it is marked as unrelated. This is useful for
   * custom executions, command-like flows or edge cases where the SQL is real
   * but page correlation is incomplete.
   */
  public function classify(?string $requestId, ?string $routeName): string {
    if (($requestId ?? '') === '' && ($routeName ?? '') === '') {
      return 'unrelated';
    }

    return 'request_bound';
  }

  /**
   * Builds a stable SQL hash.
   *
   * The hash lets the RAW section aggregate recurring statements later without
   * forcing the module to store or render the full SQL text all the time.
   */
  public function buildQueryHash(string $query): string {
    return hash('sha256', $query);
  }

}
