<?php

declare(strict_types=1);

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

use App\Framework\Mcp\Tools\RepositoryRefactoringAnalyzer;

$projectRoot = realpath(__DIR__ . '/../..');
$analyzer = new RepositoryRefactoringAnalyzer($projectRoot);

echo "=== Repository Refactoring Analyzer Test ===\n\n";

// Test 1: Analyze single repository
echo "TEST 1: Analyzing DatabaseSmartLinkRepository\n";
echo str_repeat('-', 80) . "\n";

$result = $analyzer->analyzeRepository('src/Domain/SmartLink/Repositories/DatabaseSmartLinkRepository.php');

echo "Repository: {$result['repository']}\n";
echo "Total Queries: {$result['total_queries']}\n";
echo "Factory Method Candidates: {$result['summary']['factory_method_candidates']}\n";
echo "Criteria API Candidates: {$result['summary']['criteria_api_candidates']}\n";
echo "Keep Raw SQL: {$result['summary']['keep_raw_sql']}\n";
echo "Migration Priority: {$result['migration_priority']}\n\n";

if (! empty($result['queries'])) {
    echo "Query Details:\n";
    foreach ($result['queries'] as $i => $query) {
        echo "\nQuery " . ($i + 1) . ":\n";
        echo "  Type: {$query['query_type']}\n";
        echo "  Complexity: {$query['complexity_score']}/10\n";
        echo "  Recommendation: {$query['recommendation']}\n";
        echo "  Features: " . implode(', ', $query['features']) . "\n";
        echo "  SQL Preview: " . substr($query['sql'], 0, 80) . "...\n";
    }
}

echo "\n" . str_repeat('=', 80) . "\n\n";

// Test 2: Create migration plan
echo "TEST 2: Creating Migration Plan\n";
echo str_repeat('-', 80) . "\n";

$plan = $analyzer->createMigrationPlan();

echo "Total Repositories Analyzed: {$plan['total_repositories']}\n";
echo "Repositories with Queries: {$plan['repositories_with_queries']}\n\n";

echo "Summary:\n";
echo "  Total Queries to Refactor: {$plan['summary']['total_queries_to_refactor']}\n";
echo "  Factory Method Migrations: {$plan['summary']['factory_method_migrations']}\n";
echo "  Criteria API Migrations: {$plan['summary']['criteria_api_migrations']}\n";
echo "  Keep as Raw SQL: {$plan['summary']['keep_as_raw_sql']}\n";
echo "  Estimated Effort: {$plan['summary']['estimated_effort_hours']} hours\n\n";

echo "Top 5 Repositories by Priority:\n";
foreach (array_slice($plan['migration_plan'], 0, 5) as $i => $repo) {
    echo "\n" . ($i + 1) . ". {$repo['repository']}\n";
    echo "   Priority: {$repo['migration_priority']}\n";
    echo "   Queries: {$repo['total_queries']} total\n";
    echo "   - Factory: {$repo['summary']['factory_method_candidates']}\n";
    echo "   - Criteria: {$repo['summary']['criteria_api_candidates']}\n";
    echo "   - Raw SQL: {$repo['summary']['keep_raw_sql']}\n";
}

echo "\n" . str_repeat('=', 80) . "\n\n";

// Test 3: Generate refactored code
echo "TEST 3: Generating Refactored Code\n";
echo str_repeat('-', 80) . "\n";

$refactored = $analyzer->generateRefactoredQuery(
    'SELECT * FROM smartlinks WHERE user_id = ? AND status = ? ORDER BY created_at DESC LIMIT 10',
    'findActiveLinksForUser',
    'SmartLink::class'
);

echo "Original SQL:\n{$refactored['original_sql']}\n\n";
echo "Analysis:\n";
echo "  Recommendation: {$refactored['analysis']['recommendation']}\n";
echo "  Complexity: {$refactored['analysis']['complexity_score']}/10\n";
echo "  Features: " . implode(', ', $refactored['analysis']['features']) . "\n\n";

echo "Refactored Code:\n";
echo $refactored['refactored_code'] . "\n\n";

echo "Explanation:\n";
echo $refactored['explanation'] . "\n";

echo "\n" . str_repeat('=', 80) . "\n";
echo "Tests completed successfully!\n";
