<?php

declare(strict_types=1);

namespace App\Framework\Exception\Core;

use App\Framework\Exception\ErrorCode;

/**
 * Template error codes
 *
 * Covers template rendering, processing, and view operations
 */
enum TemplateErrorCode: string implements ErrorCode
{
    case TEMPLATE_NOT_FOUND = 'TPL001';
    case RENDERING_FAILED = 'TPL002';
    case SYNTAX_ERROR = 'TPL003';
    case PROCESSOR_FAILED = 'TPL004';
    case COMPONENT_NOT_FOUND = 'TPL005';
    case INVALID_PLACEHOLDER = 'TPL006';
    case SLOT_NOT_FOUND = 'TPL007';
    case LAYOUT_NOT_FOUND = 'TPL008';
    case CIRCULAR_INCLUSION = 'TPL009';
    case DATA_BINDING_FAILED = 'TPL010';

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

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

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

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

            self::SYNTAX_ERROR,
            self::PROCESSOR_FAILED,
            self::DATA_BINDING_FAILED => ErrorSeverity::ERROR,

            self::TEMPLATE_NOT_FOUND,
            self::COMPONENT_NOT_FOUND,
            self::INVALID_PLACEHOLDER,
            self::SLOT_NOT_FOUND,
            self::LAYOUT_NOT_FOUND => ErrorSeverity::WARNING,
        };
    }

    public function getDescription(): string
    {
        return match($this) {
            self::TEMPLATE_NOT_FOUND => 'Template file not found',
            self::RENDERING_FAILED => 'Template rendering failed',
            self::SYNTAX_ERROR => 'Template syntax error detected',
            self::PROCESSOR_FAILED => 'Template processor execution failed',
            self::COMPONENT_NOT_FOUND => 'Template component not found',
            self::INVALID_PLACEHOLDER => 'Invalid placeholder syntax or reference',
            self::SLOT_NOT_FOUND => 'Template slot not found',
            self::LAYOUT_NOT_FOUND => 'Layout template not found',
            self::CIRCULAR_INCLUSION => 'Circular template inclusion detected',
            self::DATA_BINDING_FAILED => 'Template data binding failed',
        };
    }

    public function getRecoveryHint(): string
    {
        return match($this) {
            self::TEMPLATE_NOT_FOUND => 'Check template path and file existence',
            self::RENDERING_FAILED => 'Review template syntax and data structure',
            self::SYNTAX_ERROR => 'Fix template syntax according to documentation',
            self::PROCESSOR_FAILED => 'Check processor logs and template content',
            self::COMPONENT_NOT_FOUND => 'Verify component name and registration',
            self::INVALID_PLACEHOLDER => 'Use correct placeholder syntax {variable}',
            self::SLOT_NOT_FOUND => 'Define required slots in template',
            self::LAYOUT_NOT_FOUND => 'Create layout template or fix layout path',
            self::CIRCULAR_INCLUSION => 'Remove circular template inclusion chain',
            self::DATA_BINDING_FAILED => 'Ensure all required data is provided',
        };
    }

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

    public function getRetryAfterSeconds(): ?int
    {
        return null;
    }
}
