<?php

declare(strict_types=1);

use App\Framework\Mcp\Tools\FrameworkTools;

describe('FrameworkTools', function () {
    // Lazy initialization - only bootstrap when needed, reuse across all tests
    $frameworkTools = null;

    $getFrameworkTools = function () use (&$frameworkTools) {
        if ($frameworkTools === null) {
            $container = createTestContainer();
            $discoveryService = $container->get(\App\Framework\Discovery\UnifiedDiscoveryService::class);
            $compiledRoutes = $container->get(\App\Framework\Router\CompiledRoutes::class);
            $discoveryRegistry = $discoveryService->getResults();

            $frameworkTools = new FrameworkTools(
                $container,
                $discoveryService,
                $compiledRoutes,
                $discoveryRegistry
            );
        }

        return $frameworkTools;
    };

    describe('analyze_routes', function () use ($getFrameworkTools) {
        it('returns route analysis with compiled and discovered routes', function () use ($getFrameworkTools) {
            $tools = $getFrameworkTools();
            $result = $tools->analyzeRoutes();

            expect($result)->toHaveKeys(['compiled_routes', 'discovered_routes', 'summary']);
            expect($result['compiled_routes'])->toHaveKeys(['named_routes', 'static_routes', 'total']);
            expect($result['discovered_routes'])->toHaveKeys(['routes', 'total']);
            expect($result['summary'])->toHaveKeys(['compiled_total', 'discovered_total', 'context']);
        });
    });

    describe('analyze_container_bindings', function () use ($getFrameworkTools) {
        it('returns comprehensive container analysis', function () use ($getFrameworkTools) {
            $tools = $getFrameworkTools();
            $result = $tools->analyzeContainerBindings();

            expect($result)->toHaveKeys(['summary', 'bindings', 'singletons', 'instances', 'issues', 'recommendations']);
            expect($result['summary'])->toHaveKeys(['bindings', 'singletons_marked', 'instances', 'issues']);
        });
    });

    describe('framework_health_check', function () use ($getFrameworkTools) {
        it('performs health check of framework components', function () use ($getFrameworkTools) {
            $tools = $getFrameworkTools();
            $result = $tools->frameworkHealthCheck();

            expect($result)->toHaveKeys(['status', 'components', 'timestamp']);
            expect($result['status'])->toBe('completed');
            expect($result['components'])->toHaveKeys(['container', 'routes', 'discovery_service']);
        });
    });

    describe('list_framework_modules', function () use ($getFrameworkTools) {
        it('lists all framework modules with file counts', function () use ($getFrameworkTools) {
            $tools = $getFrameworkTools();
            $result = $tools->listFrameworkModules();

            expect($result)->toHaveKeys(['modules', 'total_modules']);
            expect($result['modules'])->toBeArray();
            expect($result['total_modules'])->toBeInt();
        });
    });
});
