<?php

declare(strict_types=1);

namespace App\Framework\Exception\Core;

use App\Framework\Exception\ErrorCode;

/**
 * Performance error codes
 *
 * Covers performance degradation, resource exhaustion, and optimization issues
 */
enum PerformanceErrorCode: string implements ErrorCode
{
    case MEMORY_LIMIT_EXCEEDED = 'PERF001';
    case EXECUTION_TIME_EXCEEDED = 'PERF002';
    case SLOW_QUERY_DETECTED = 'PERF003';
    case HIGH_CPU_USAGE = 'PERF004';
    case RESOURCE_EXHAUSTION = 'PERF005';

    public function getValue(): string
    {
        return $this->value;
    }

    public function getCategory(): string
    {
        return 'PERF';
    }

    public function getNumericCode(): int
    {
        return (int) substr($this->value, -3);
    }

    public function getSeverity(): ErrorSeverity
    {
        return match($this) {
            self::MEMORY_LIMIT_EXCEEDED,
            self::RESOURCE_EXHAUSTION => ErrorSeverity::CRITICAL,

            self::EXECUTION_TIME_EXCEEDED,
            self::HIGH_CPU_USAGE => ErrorSeverity::ERROR,

            self::SLOW_QUERY_DETECTED => ErrorSeverity::WARNING,
        };
    }

    public function getDescription(): string
    {
        return match($this) {
            self::MEMORY_LIMIT_EXCEEDED => 'Memory limit exceeded',
            self::EXECUTION_TIME_EXCEEDED => 'Execution time limit exceeded',
            self::SLOW_QUERY_DETECTED => 'Slow database query detected',
            self::HIGH_CPU_USAGE => 'High CPU usage detected',
            self::RESOURCE_EXHAUSTION => 'System resources exhausted',
        };
    }

    public function getRecoveryHint(): string
    {
        return match($this) {
            self::MEMORY_LIMIT_EXCEEDED => 'Increase memory limit or optimize memory usage',
            self::EXECUTION_TIME_EXCEEDED => 'Increase timeout or optimize execution',
            self::SLOW_QUERY_DETECTED => 'Optimize query or add database indices',
            self::HIGH_CPU_USAGE => 'Optimize algorithms or increase CPU resources',
            self::RESOURCE_EXHAUSTION => 'Free up resources or increase system capacity',
        };
    }

    public function isRecoverable(): bool
    {
        return match($this) {
            self::MEMORY_LIMIT_EXCEEDED => false,
            default => true,
        };
    }

    public function getRetryAfterSeconds(): ?int
    {
        return match($this) {
            self::RESOURCE_EXHAUSTION => 60,
            default => null,
        };
    }
}
