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

declare(strict_types=1);

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

use App\Application\LiveComponents\ActivityFeedComponent;
use App\Application\LiveComponents\AutocompleteComponent;
use App\Application\LiveComponents\ChartComponent;
use App\Application\LiveComponents\InfiniteScrollComponent;
use App\Application\LiveComponents\MetricsDashboardComponent;
use App\Application\LiveComponents\Services\DemoActivityDataProvider;
use App\Application\LiveComponents\Services\DemoMetricsDataProvider;
use App\Application\LiveComponents\Services\DemoScrollDataProvider;
use App\Application\LiveComponents\Services\DemoSuggestionProvider;

echo "Testing Refactored LiveComponents\n";
echo "==================================\n\n";

// Test 1: ChartComponent
echo "1. Testing ChartComponent...\n";

try {
    $chart = new ChartComponent(
        id: 'chart-test',
        initialData: [
            'data_source' => 'demo',
            'chart_type' => 'line',
            'data_range' => '24h',
        ]
    );

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

    // Test action method
    $result = $chart->refreshData();
    echo "   ✓ refreshData() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "   - Keys: " . implode(', ', array_keys($result)) . "\n";

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

echo "\n";

// Test 2: AutocompleteComponent
echo "2. Testing AutocompleteComponent...\n";

try {
    $provider = new DemoSuggestionProvider();
    $autocomplete = new AutocompleteComponent(
        id: 'autocomplete-test',
        suggestionProvider: $provider,
        initialData: ['context' => 'general']
    );

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

    // Test action method
    $result = $autocomplete->getSuggestions('test', 'general');
    echo "   ✓ getSuggestions() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "   - Keys: " . implode(', ', array_keys($result)) . "\n";

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

echo "\n";

// Test 3: InfiniteScrollComponent
echo "3. Testing InfiniteScrollComponent...\n";

try {
    $provider = new DemoScrollDataProvider();
    $scroll = new InfiniteScrollComponent(
        id: 'scroll-test',
        dataProvider: $provider,
        initialData: ['page_size' => 20]
    );

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

    // Test action method
    $result = $scroll->loadMore();
    echo "   ✓ loadMore() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "   - Keys: " . implode(', ', array_keys($result)) . "\n";

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

echo "\n";

// Test 4: ActivityFeedComponent
echo "4. Testing ActivityFeedComponent...\n";

try {
    $provider = new DemoActivityDataProvider();
    $activity = new ActivityFeedComponent(
        id: 'activity-test',
        dataProvider: $provider,
        initialData: ['filter' => 'all']
    );

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

    // Test action method
    $result = $activity->setFilter('comments');
    echo "   ✓ setFilter() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "   - Keys: " . implode(', ', array_keys($result)) . "\n";

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

echo "\n";

// Test 5: MetricsDashboardComponent
echo "5. Testing MetricsDashboardComponent...\n";

try {
    $provider = new DemoMetricsDataProvider();
    $metrics = new MetricsDashboardComponent(
        id: 'metrics-test',
        dataProvider: $provider,
        initialData: ['time_range' => '30d']
    );

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

    // Test action method
    $result = $metrics->refreshMetrics();
    echo "   ✓ refreshMetrics() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "   - Keys: " . implode(', ', array_keys($result)) . "\n";

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

echo "\n";
echo "==================================\n";
echo "All component tests completed!\n";
