#!/usr/bin/env php
<?php

declare(strict_types=1);

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

use App\Application\LiveComponents\DynamicFormComponent;

echo "Testing DynamicFormComponent Migration\n";
echo "=======================================\n\n";

try {
    $component = new DynamicFormComponent(
        id: 'dynamic-form-test',
        initialData: ['current_step' => 1, 'form_data' => [], 'total_steps' => 4]
    );

    echo "✓ DynamicFormComponent created successfully\n";
    echo "  - ID: " . $component->getId() . "\n\n";

    // Test nextStep() method
    echo "Testing nextStep() method...\n";
    $result = $component->nextStep();
    echo "  ✓ nextStep() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Current step: " . $result['current_step'] . "\n";
    echo "  - Total steps: " . $result['total_steps'] . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n\n";

    // Test previousStep() method
    $component2 = new DynamicFormComponent(
        id: 'dynamic-form-test-2',
        initialData: ['current_step' => 3, 'form_data' => []]
    );

    echo "Testing previousStep() method...\n";
    $result = $component2->previousStep();
    echo "  ✓ previousStep() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Current step: " . $result['current_step'] . "\n\n";

    // Test updateField() method
    echo "Testing updateField() method...\n";
    $result = $component->updateField('first_name', 'John');
    echo "  ✓ updateField() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Field value: " . ($result['form_data']['first_name'] ?? 'not set') . "\n\n";

    // Test submit() method
    $component3 = new DynamicFormComponent(
        id: 'dynamic-form-test-3',
        initialData: [
            'current_step' => 4,
            'form_data' => [
                'first_name' => 'John',
                'last_name' => 'Doe',
                'email' => 'john@example.com',
                'account_type' => 'personal',
            ],
        ]
    );

    echo "Testing submit() method...\n";
    $result = $component3->submit();
    echo "  ✓ submit() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Submitted: " . ($result['submitted'] ? 'true' : 'false') . "\n";
    echo "  - Submission ID: " . ($result['submission_id'] ?? 'none') . "\n\n";

    // Test reset() method
    echo "Testing reset() method...\n";
    $result = $component2->reset();
    echo "  ✓ reset() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Current step: " . $result['current_step'] . "\n";
    echo "  - Form data empty: " . (empty($result['form_data']) ? 'YES' : 'NO') . "\n\n";

    echo "=======================================\n";
    echo "✅ All DynamicFormComponent tests passed!\n";

} catch (\Throwable $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
    echo "   File: " . $e->getFile() . ":" . $e->getLine() . "\n";
    exit(1);
}
