<?php

declare(strict_types=1);

namespace App\Framework\Exception\Core;

use App\Framework\Exception\ErrorCode;

/**
 * Validation error codes
 *
 * Covers input validation, business rule violations, and data integrity checks
 */
enum ValidationErrorCode: string implements ErrorCode
{
    case INVALID_INPUT = 'VAL001';
    case REQUIRED_FIELD_MISSING = 'VAL002';
    case INVALID_FORMAT = 'VAL003';
    case VALUE_OUT_OF_RANGE = 'VAL004';
    case DUPLICATE_VALUE = 'VAL005';
    case BUSINESS_RULE_VIOLATION = 'VAL006';
    case INVALID_STATE_TRANSITION = 'VAL007';
    case VALIDATION_FAILED = 'VAL008';

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

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

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

    public function getSeverity(): ErrorSeverity
    {
        return match($this) {
            self::BUSINESS_RULE_VIOLATION,
            self::INVALID_STATE_TRANSITION => ErrorSeverity::ERROR,

            self::INVALID_INPUT,
            self::REQUIRED_FIELD_MISSING,
            self::INVALID_FORMAT,
            self::VALUE_OUT_OF_RANGE,
            self::DUPLICATE_VALUE,
            self::VALIDATION_FAILED => ErrorSeverity::WARNING,
        };
    }

    public function getDescription(): string
    {
        return match($this) {
            self::INVALID_INPUT => 'Input data is invalid',
            self::REQUIRED_FIELD_MISSING => 'Required field is missing',
            self::INVALID_FORMAT => 'Data format is invalid',
            self::VALUE_OUT_OF_RANGE => 'Value is outside acceptable range',
            self::DUPLICATE_VALUE => 'Duplicate value detected',
            self::BUSINESS_RULE_VIOLATION => 'Business rule violation occurred',
            self::INVALID_STATE_TRANSITION => 'Invalid state transition attempted',
            self::VALIDATION_FAILED => 'Validation check failed',
        };
    }

    public function getRecoveryHint(): string
    {
        return match($this) {
            self::INVALID_INPUT => 'Check input data format and values',
            self::REQUIRED_FIELD_MISSING => 'Provide all required fields',
            self::INVALID_FORMAT => 'Correct data format according to specification',
            self::VALUE_OUT_OF_RANGE => 'Provide value within acceptable range',
            self::DUPLICATE_VALUE => 'Use unique value or update existing entry',
            self::BUSINESS_RULE_VIOLATION => 'Review business rules and adjust input',
            self::INVALID_STATE_TRANSITION => 'Check current state and allowed transitions',
            self::VALIDATION_FAILED => 'Review validation errors and correct input',
        };
    }

    public function isRecoverable(): bool
    {
        return true;
    }

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