<?php

declare(strict_types=1);

namespace App\Framework\Mcp\ValueObjects;

/**
 * Value Object representing the results of codebase analysis
 */
final readonly class CodebaseAnalysisResult
{
    /**
     * @param array<ComponentInfo> $controllers
     * @param array<ComponentInfo> $services
     * @param array<ComponentInfo> $valueObjects
     * @param array<ComponentInfo> $repositories
     * @param array<RouteInfo> $routes
     * @param array<InitializerInfo> $initializers
     * @param array<CommandInfo> $commands
     * @param array<EventHandlerInfo> $eventHandlers
     * @param array<McpToolInfo> $mcpTools
     * @param array<InterfaceImplementationInfo> $interfaceImplementations
     * @param array<string, mixed> $statistics
     * @param float $executionTimeMs
     */
    public function __construct(
        public array $controllers = [],
        public array $services = [],
        public array $valueObjects = [],
        public array $repositories = [],
        public array $routes = [],
        public array $initializers = [],
        public array $commands = [],
        public array $eventHandlers = [],
        public array $mcpTools = [],
        public array $interfaceImplementations = [],
        public array $statistics = [],
        public float $executionTimeMs = 0.0
    ) {
    }

    public static function empty(): self
    {
        return new self();
    }

    public function toArray(): array
    {
        return [
            'controllers' => array_map(fn ($c) => $c->toArray(), $this->controllers),
            'services' => array_map(fn ($s) => $s->toArray(), $this->services),
            'value_objects' => array_map(fn ($vo) => $vo->toArray(), $this->valueObjects),
            'repositories' => array_map(fn ($r) => $r->toArray(), $this->repositories),
            'routes' => array_map(fn ($r) => $r->toArray(), $this->routes),
            'initializers' => array_map(fn ($i) => $i->toArray(), $this->initializers),
            'commands' => array_map(fn ($c) => $c->toArray(), $this->commands),
            'event_handlers' => array_map(fn ($e) => $e->toArray(), $this->eventHandlers),
            'mcp_tools' => array_map(fn ($m) => $m->toArray(), $this->mcpTools),
            'interface_implementations' => array_map(fn ($i) => $i->toArray(), $this->interfaceImplementations),
            'statistics' => $this->statistics,
            'execution_time_ms' => $this->executionTimeMs,
        ];
    }

    public function getTotalComponents(): int
    {
        return count($this->controllers)
            + count($this->services)
            + count($this->valueObjects)
            + count($this->repositories)
            + count($this->routes)
            + count($this->initializers)
            + count($this->commands)
            + count($this->eventHandlers)
            + count($this->mcpTools)
            + count($this->interfaceImplementations);
    }

    public function isEmpty(): bool
    {
        return $this->getTotalComponents() === 0;
    }
}
