<?php

declare(strict_types=1);

namespace App\Framework\DI;

use App\Framework\Core\AttributeMapper;
use App\Framework\Reflection\WrappedReflectionClass;
use App\Framework\Reflection\WrappedReflectionMethod;

final readonly class InitializerMapper implements AttributeMapper
{
    public function getAttributeClass(): string
    {
        return Initializer::class;
    }

    public function map(WrappedReflectionClass|WrappedReflectionMethod $reflectionTarget, object $attributeInstance): array
    {
        /** @var Initializer $attributeInstance */

        if ($reflectionTarget instanceof WrappedReflectionMethod) {
            return [
                'class' => $reflectionTarget->getDeclaringClass()->getFullyQualified(),
                'return' => $this->getReturnTypeName($reflectionTarget),
                'method' => $reflectionTarget->getName(),
                'contexts' => $attributeInstance->contexts,
            ];
        }

        // For class-level initializers (if needed in the future)
        if ($reflectionTarget instanceof WrappedReflectionClass) {
            return [
                'class' => $reflectionTarget->getName()->getFullyQualified(),
                'return' => null,
                'method' => null,
                'contexts' => $attributeInstance->contexts,
            ];
        }

        return [];
    }

    private function getReturnTypeName(WrappedReflectionMethod $method): ?string
    {
        return $method->getReturnType();
    }
}
