<?php

declare(strict_types=1);

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Framework\Mcp\ValueObjects\CodebaseAnalysisResult;
use App\Framework\Mcp\ValueObjects\CodebaseQuery;
use App\Framework\Mcp\ValueObjects\RouteInfo;

echo "=== CodebaseAnalyzer Value Objects Test ===\n\n";

// Test 1: CodebaseQuery Creation
echo "--- Test 1: CodebaseQuery Factory Methods ---\n";
$controllersQuery = CodebaseQuery::forControllers();
echo "✅ Controllers Query: " . json_encode($controllersQuery->toArray(), JSON_PRETTY_PRINT) . "\n\n";

$routesQuery = CodebaseQuery::forRoutes();
echo "✅ Routes Query: attribute_types = " . count($routesQuery->attributeTypes) . "\n\n";

$servicesQuery = CodebaseQuery::forServices();
echo "✅ Services Query: classNamePatterns = " . implode(', ', $servicesQuery->classNamePatterns) . "\n\n";

// Test 2: Custom Query
echo "--- Test 2: Custom CodebaseQuery ---\n";
$customQuery = CodebaseQuery::fromArray([
    'patterns' => ['UserController', 'ProductService'],
    'attribute_types' => ['App\Framework\Attributes\Route'],
    'max_results' => 50,
    'include_tests' => true,
]);

echo "✅ Custom Query Created:\n";
echo "  - Patterns: " . implode(', ', $customQuery->patterns) . "\n";
echo "  - Max Results: {$customQuery->maxResults}\n";
echo "  - Include Tests: " . ($customQuery->includeTests ? 'Yes' : 'No') . "\n";
echo "  - Has Attribute Search: " . ($customQuery->hasAttributeSearch() ? 'Yes' : 'No') . "\n\n";

// Test 3: CodebaseAnalysisResult Creation
echo "--- Test 3: CodebaseAnalysisResult Construction ---\n";
$sampleRoutes = [
    new RouteInfo(
        path: '/api/users',
        httpMethod: 'GET',
        controller: 'App\Application\Api\UserController',
        action: 'listUsers'
    ),
    new RouteInfo(
        path: '/api/users/{id}',
        httpMethod: 'GET',
        controller: 'App\Application\Api\UserController',
        action: 'getUser',
        parameters: ['id']
    ),
];

$result = new CodebaseAnalysisResult(
    routes: $sampleRoutes,
    statistics: [
        'total_routes' => count($sampleRoutes),
        'execution_time_ms' => 42.5,
    ],
    executionTimeMs: 42.5
);

echo "✅ Analysis Result Created:\n";
echo "  - Total Components: {$result->getTotalComponents()}\n";
echo "  - Routes Count: " . count($result->routes) . "\n";
echo "  - Is Empty: " . ($result->isEmpty() ? 'Yes' : 'No') . "\n";
echo "  - Execution Time: {$result->executionTimeMs}ms\n\n";

// Test 4: Result to Array Conversion
echo "--- Test 4: Result Serialization ---\n";
$arrayResult = $result->toArray();
echo "✅ Array keys: " . implode(', ', array_keys($arrayResult)) . "\n";
echo "  - Routes in array: " . count($arrayResult['routes']) . "\n";
echo "  - First route path: " . $arrayResult['routes'][0]['path'] . "\n\n";

echo "=== All Value Object Tests Completed Successfully ✅ ===\n";
