<?php

declare(strict_types=1);

namespace Drupal\native_observability_dashboard\Service;

/**
 * Formats dashboard metric values for UI rendering.
 */
final class MetricValueFormatter {

  /**
   * Formats a duration value expressed in milliseconds.
   *
   * @param float $milliseconds
   *   The duration in milliseconds.
   *
   * @return array
   *   A formatted value array with value and unit keys.
   */
  public function formatDuration(float $milliseconds): array {
    if ($milliseconds >= 1000) {
      return [
        'formatted_value' => number_format($milliseconds / 1000, 2),
        'unit' => 's',
      ];
    }

    return [
      'formatted_value' => number_format($milliseconds, 0),
      'unit' => 'ms',
    ];
  }

  /**
   * Formats a percentage value.
   *
   * @param float $value
   *   The raw percentage value.
   *
   * @return array
   *   A formatted value array with value and unit keys.
   */
  public function formatPercentage(float $value): array {
    return [
      'formatted_value' => number_format($value, 2),
      'unit' => '%',
    ];
  }

  /**
   * Formats a generic numeric value.
   *
   * @param float $value
   *   The raw numeric value.
   * @param int $precision
   *   The required decimal precision.
   *
   * @return array
   *   A formatted value array with value and unit keys.
   */
  public function formatNumber(float $value, int $precision = 0): array {
    return [
      'formatted_value' => number_format($value, $precision),
      'unit' => '',
    ];
  }

}
