<?php

declare(strict_types=1);

/**
 * Test script for nullable bool flags
 */

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

use App\Framework\Console\CommandParameterResolver;
use App\Framework\Console\ConsoleOutput;
use App\Framework\Console\MethodSignatureAnalyzer;

// Test class with nullable bool flags
class FlagTestCommand
{
    // Nullable bool flags
    public function withFlags(
        string $environment,
        ?bool $verbose = null,
        ?bool $force = null,
        ?bool $dryRun = null
    ): int {
        echo "Environment: {$environment}\n";
        echo "Verbose: " . ($verbose === null ? 'null' : ($verbose ? 'true' : 'false')) . "\n";
        echo "Force: " . ($force === null ? 'null' : ($force ? 'true' : 'false')) . "\n";
        echo "DryRun: " . ($dryRun === null ? 'null' : ($dryRun ? 'true' : 'false')) . "\n";
        return 0;
    }

    // Mixed: typed params + flags
    public function mixedWithFlags(
        string $name,
        int $age,
        ?bool $verified = null,
        ?bool $active = null
    ): int {
        echo "Name: {$name}, Age: {$age}\n";
        echo "Verified: " . ($verified === null ? 'null' : ($verified ? 'true' : 'false')) . "\n";
        echo "Active: " . ($active === null ? 'null' : ($active ? 'true' : 'false')) . "\n";
        return 0;
    }
}

echo "Testing Nullable Bool Flags\n";
echo "===========================\n\n";

$resolver = new CommandParameterResolver(new MethodSignatureAnalyzer());
$command = new FlagTestCommand();
$output = new ConsoleOutput();

// Test 1: No flags provided
echo "Test 1: No flags (all should be null)\n";
echo "Command: deploy production\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'withFlags');
$params = $resolver->resolveParameters($method, ['production'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

// Test 2: One flag provided
echo "Test 2: One flag --verbose\n";
echo "Command: deploy production --verbose\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'withFlags');
$params = $resolver->resolveParameters($method, ['production', '--verbose'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

// Test 3: Multiple flags
echo "Test 3: Multiple flags --verbose --force\n";
echo "Command: deploy production --verbose --force\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'withFlags');
$params = $resolver->resolveParameters($method, ['production', '--verbose', '--force'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

// Test 4: All flags
echo "Test 4: All flags --verbose --force --dry-run\n";
echo "Command: deploy production --verbose --force --dry-run\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'withFlags');
$params = $resolver->resolveParameters($method, ['production', '--verbose', '--force', '--dry-run'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

// Test 5: Mixed params + flags (no flags)
echo "Test 5: Mixed parameters without flags\n";
echo "Command: user Alice 25\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'mixedWithFlags');
$params = $resolver->resolveParameters($method, ['Alice', '25'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

// Test 6: Mixed params + flags (with flags)
echo "Test 6: Mixed parameters with flags\n";
echo "Command: user Alice 25 --verified --active\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'mixedWithFlags');
$params = $resolver->resolveParameters($method, ['Alice', '25', '--verified', '--active'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

// Test 7: Mixed params + one flag
echo "Test 7: Mixed parameters with one flag\n";
echo "Command: user Bob 30 --verified\n";
$method = new ReflectionMethod(FlagTestCommand::class, 'mixedWithFlags');
$params = $resolver->resolveParameters($method, ['Bob', '30', '--verified'], null, $output);
echo "Resolved params count: " . count($params) . "\n";
$result = $method->invokeArgs($command, $params);
echo "Result: {$result}\n\n";

echo "✅ All tests completed!\n";
echo "\nExpected behavior:\n";
echo "- Flags without '--' prefix: null\n";
echo "- Flags with '--' prefix: true\n";
echo "- No boolean false (only null or true)\n";
