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

declare(strict_types=1);

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

use App\Application\LiveComponents\ProductFilterComponent;

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

try {
    $component = new ProductFilterComponent(
        id: 'product-filter-test',
        initialData: [
            'active_filters' => [],
            'available_categories' => ['electronics', 'clothing', 'books'],
            'available_brands' => ['Apple', 'Samsung', 'Sony'],
            'available_colors' => ['red', 'blue', 'green'],
            'available_sizes' => ['S', 'M', 'L', 'XL'],
            'price_range' => ['min' => 0, 'max' => 1000],
            'sort_by' => 'relevance',
        ]
    );

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

    // Test setCategory() method
    echo "Testing setCategory() method...\n";
    $result = $component->setCategory('electronics');
    echo "  ✓ setCategory() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Active filters has category: " . (isset($result['active_filters']['category']) ? 'YES' : 'NO') . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n\n";

    // Test setPriceRange() method
    echo "Testing setPriceRange() method...\n";
    $result = $component->setPriceRange(10.0, 500.0);
    echo "  ✓ setPriceRange() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Price range: {$result['price_range']['min']} - {$result['price_range']['max']}\n\n";

    // Test toggleBrand() method
    echo "Testing toggleBrand() method...\n";
    $result = $component->toggleBrand('Apple');
    echo "  ✓ toggleBrand() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Has brands in filters: " . (isset($result['active_filters']['brands']) ? 'YES' : 'NO') . "\n\n";

    // Test setMinRating() method
    echo "Testing setMinRating() method...\n";
    $result = $component->setMinRating(4);
    echo "  ✓ setMinRating() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Has min_rating in filters: " . (isset($result['active_filters']['min_rating']) ? 'YES' : 'NO') . "\n\n";

    // Test toggleInStock() method
    echo "Testing toggleInStock() method...\n";
    $result = $component->toggleInStock();
    echo "  ✓ toggleInStock() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Has in_stock_only filter: " . (isset($result['active_filters']['in_stock_only']) ? 'YES' : 'NO') . "\n\n";

    // Test setSortBy() method
    echo "Testing setSortBy() method...\n";
    $result = $component->setSortBy('price');
    echo "  ✓ setSortBy() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Sort by: {$result['sort_by']}\n\n";

    // Test toggleSize() method
    echo "Testing toggleSize() method...\n";
    $result = $component->toggleSize('XL');
    echo "  ✓ toggleSize() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Has sizes in filters: " . (isset($result['active_filters']['sizes']) ? 'YES' : 'NO') . "\n\n";

    // Test toggleColor() method
    echo "Testing toggleColor() method...\n";
    $result = $component->toggleColor('blue');
    echo "  ✓ toggleColor() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Has colors in filters: " . (isset($result['active_filters']['colors']) ? 'YES' : 'NO') . "\n\n";

    // Test clearFilters() method
    $component2 = new ProductFilterComponent(
        id: 'product-filter-test-2',
        initialData: [
            'active_filters' => ['category' => 'electronics', 'min_rating' => 4],
        ]
    );

    echo "Testing clearFilters() method...\n";
    $result = $component2->clearFilters();
    echo "  ✓ clearFilters() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Filters cleared: " . (empty($result['active_filters']) ? 'YES' : 'NO') . "\n\n";

    // Test removeFilter() method
    echo "Testing removeFilter() method...\n";
    $result = $component2->removeFilter('category');
    echo "  ✓ removeFilter() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Category removed: " . (! isset($result['active_filters']['category']) ? 'YES' : 'NO') . "\n\n";

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

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