<?php

declare(strict_types=1);

namespace App\Framework\Core;

use App\Framework\Attributes\Route;
use ReflectionMethod;

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

    public function map(object $reflectionTarget, object $attributeInstance): ?array
    {
        if (! $reflectionTarget instanceof ReflectionMethod || ! $attributeInstance instanceof Route) {
            return null;
        }

        $attributes = [];
        foreach ($reflectionTarget->getAttributes() as $attribute) {
            if ($attribute->getName() !== Route::class) {
                $attributes[] = $attribute->getName();
            }
        };

        return [
            'class' => $reflectionTarget->getDeclaringClass()->getName(),
            'method' => $reflectionTarget->getName(),
            'http_method' => $attributeInstance->method,
            'path' => $attributeInstance->path,
            'name' => $attributeInstance->name,
            'attributes' => $attributes
        ];
    }
}
