<?php

declare(strict_types=1);

namespace App\Framework\Discovery\Plugins;

use App\Framework\Core\AttributeMapper;
use App\Framework\Discovery\Contracts\DiscoveryPlugin;
use App\Framework\Discovery\Contracts\DiscoveryVisitor;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Discovery\ValueObjects\DiscoveryContext;
use App\Framework\Discovery\Visitors\AttributeVisitor;

/**
 * Plugin for discovering PHP attributes
 */
final readonly class AttributeDiscoveryPlugin implements DiscoveryPlugin
{
    /** @var array<AttributeMapper> */
    private array $mappers;

    /** @var array<DiscoveryVisitor> */
    private array $visitors;

    public function __construct(array $attributeMappers = [])
    {
        $this->mappers = $attributeMappers;
        $this->visitors = [
            new AttributeVisitor($attributeMappers),
        ];
    }

    public function getName(): string
    {
        return 'attributes';
    }

    public function initialize(DiscoveryContext $context): void
    {
        // Reset visitors for new discovery
        foreach ($this->visitors as $visitor) {
            $visitor->reset();
        }
    }

    public function finalize(DiscoveryContext $context, DiscoveryRegistry $registry): void
    {
        // Attributes are already in the registry, no finalization needed
    }

    public function getVisitors(): array
    {
        return $this->visitors;
    }

    public function isEnabled(DiscoveryContext $context): bool
    {
        // Always enabled unless explicitly disabled
        return true;
    }
}
