<?php

declare(strict_types=1);

namespace App\Framework\Metrics;

/**
 * Standard metric suffixes for complex metric types
 */
enum MetricSuffix: string
{
    // Histogram suffixes
    case BUCKET = '_bucket';
    case SUM = '_sum';
    case COUNT = '_count';

    // Summary suffixes (for future use)
    case QUANTILE = '_quantile';

    // Info suffix (for future use)
    case INFO = '_info';

    /**
     * Check if a metric name has this suffix
     */
    public function isInName(string $name): bool
    {
        return str_ends_with($name, $this->value);
    }

    /**
     * Remove this suffix from a metric name
     */
    public function removeFrom(string $name): string
    {
        if ($this->isInName($name)) {
            return substr($name, 0, -strlen($this->value));
        }

        return $name;
    }

    /**
     * Add this suffix to a metric name
     */
    public function addTo(string $name): string
    {
        if (! $this->isInName($name)) {
            return $name . $this->value;
        }

        return $name;
    }

    /**
     * Get all histogram-related suffixes
     * @return self[]
     */
    public static function histogramSuffixes(): array
    {
        return [
            self::BUCKET,
            self::SUM,
            self::COUNT,
        ];
    }

    /**
     * Check if name has any histogram suffix
     */
    public static function hasHistogramSuffix(string $name): bool
    {
        foreach (self::histogramSuffixes() as $suffix) {
            if ($suffix->isInName($name)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Get base name by removing any known suffix
     */
    public static function getBaseName(string $name): string
    {
        foreach (self::cases() as $suffix) {
            if ($suffix->isInName($name)) {
                return $suffix->removeFrom($name);
            }
        }

        return $name;
    }
}
