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

declare(strict_types=1);

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

use App\Application\LiveComponents\CommentThreadComponent;

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

try {
    $component = new CommentThreadComponent(
        id: 'comment-thread-test',
        initialData: []
    );

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

    // Test addComment() method
    echo "Testing addComment() method...\n";
    $result = $component->addComment('Test comment', 'user1', 'John Doe');
    echo "  ✓ addComment() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Comments count: " . count($result['comments']) . "\n";
    echo "  - Keys: " . implode(', ', array_keys($result)) . "\n\n";

    // Test editComment() method
    $component2 = new CommentThreadComponent(
        id: 'comment-thread-test-2',
        initialData: ['comments' => [[
            'id' => 'comment-1',
            'content' => 'Original comment',
            'author_id' => 'user1',
            'author_name' => 'John Doe',
            'parent_id' => null,
            'created_at' => time(),
            'updated_at' => null,
            'reactions' => [],
            'reply_count' => 0,
            'is_edited' => false,
        ]]]
    );

    echo "Testing editComment() method...\n";
    $result = $component2->editComment('comment-1', 'Edited comment');
    echo "  ✓ editComment() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Updated content: " . $result['comments'][0]['content'] . "\n";
    echo "  - Is edited: " . ($result['comments'][0]['is_edited'] ? 'true' : 'false') . "\n\n";

    // Test deleteComment() method
    echo "Testing deleteComment() method...\n";
    $result = $component2->deleteComment('comment-1');
    echo "  ✓ deleteComment() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Comments count after delete: " . count($result['comments']) . "\n\n";

    // Test addReaction() method
    $component3 = new CommentThreadComponent(
        id: 'comment-thread-test-3',
        initialData: ['comments' => [[
            'id' => 'comment-1',
            'content' => 'Test comment',
            'author_id' => 'user1',
            'author_name' => 'John Doe',
            'parent_id' => null,
            'created_at' => time(),
            'updated_at' => null,
            'reactions' => [],
            'reply_count' => 0,
            'is_edited' => false,
        ]]]
    );

    echo "Testing addReaction() method...\n";
    $result = $component3->addReaction('comment-1', 'like', 'user2');
    echo "  ✓ addReaction() returns array: " . (is_array($result) ? 'YES' : 'NO') . "\n";
    echo "  - Reactions count: " . count($result['comments'][0]['reactions']) . "\n\n";

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

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

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