<?php

declare(strict_types=1);

namespace App\Framework\Router\ValueObjects;

use App\Framework\Reflection\WrappedReflectionParameter;

/**
 * Type-safe representation of a method parameter
 */
final readonly class MethodParameter
{
    public function __construct(
        public string $name,
        public ?string $type,
        public bool $isBuiltin,
        public bool $hasDefault,
        public mixed $default = null
    ) {
    }

    /**
     * Create from Framework Reflection Parameter
     */
    public static function fromWrappedParameter(WrappedReflectionParameter $parameter): self
    {
        return new self(
            name: $parameter->getName(),
            type: $parameter->getTypeName(),  // Use getTypeName() for string type instead of getType()
            isBuiltin: $parameter->isBuiltin(),
            hasDefault: $parameter->hasDefaultValue(),
            default: $parameter->hasDefaultValue() ? $parameter->getDefaultValue() : null
        );
    }

    /**
     * Check if this parameter is required (no default value)
     */
    public function isRequired(): bool
    {
        return ! $this->hasDefault;
    }

    /**
     * Check if this is a specific type
     */
    public function isOfType(string $expectedType): bool
    {
        return $this->type === $expectedType;
    }

    /**
     * Convert to legacy array format for backward compatibility
     * @deprecated Use object properties instead
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'type' => $this->type,
            'isBuiltin' => $this->isBuiltin,
            'hasDefault' => $this->hasDefault,
            'default' => $this->default,
            'required' => $this->isRequired(),
        ];
    }
}
