<?php

declare(strict_types=1);

namespace App\Framework\Exception\Core;

use App\Framework\Exception\ErrorCode;

/**
 * System-level error codes
 *
 * Covers system configuration, dependencies, and initialization
 */
enum SystemErrorCode: string implements ErrorCode
{
    case CONFIG_MISSING = 'SYS001';
    case CONFIG_INVALID = 'SYS002';
    case DEPENDENCY_MISSING = 'SYS003';
    case RESOURCE_EXHAUSTED = 'SYS004';
    case INITIALIZATION_FAILED = 'SYS005';

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

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

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

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

            self::CONFIG_MISSING,
            self::CONFIG_INVALID,
            self::DEPENDENCY_MISSING => ErrorSeverity::ERROR,
        };
    }

    public function getDescription(): string
    {
        return match($this) {
            self::CONFIG_MISSING => 'Required configuration is missing',
            self::CONFIG_INVALID => 'Configuration contains invalid values',
            self::DEPENDENCY_MISSING => 'Required system dependency is not available',
            self::RESOURCE_EXHAUSTED => 'System resources are exhausted',
            self::INITIALIZATION_FAILED => 'System initialization failed',
        };
    }

    public function getRecoveryHint(): string
    {
        return match($this) {
            self::CONFIG_MISSING => 'Check configuration files and environment variables',
            self::CONFIG_INVALID => 'Review configuration values and fix invalid entries',
            self::DEPENDENCY_MISSING => 'Install missing dependencies or check system requirements',
            self::RESOURCE_EXHAUSTED => 'Free up system resources or increase limits',
            self::INITIALIZATION_FAILED => 'Check system startup logs and fix initialization issues',
        };
    }

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

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