<?php
define('WP_USE_THEMES', false);
require('../../../wp-load.php');

echo "=== Testing Livestream Rendering ===\n\n";

// Query für Videos mit Livestream
$args = array(
    'post_type' => 'video',
    'posts_per_page' => 2,
    'meta_query' => array(
        array(
            'key' => 'video_is_livestream',
            'value' => '1',
            'compare' => '='
        )
    )
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        
        $post_id = get_the_ID();
        $profile_url = get_post_meta($post_id, 'video_livestream_profile_url', true);
        $player_url = get_post_meta($post_id, 'video_livestream_player_url', true);
        $youtube_channel_id = get_post_meta($post_id, 'video_youtube_channel_id', true);
        
        echo "Post ID: $post_id\n";
        echo "Title: " . get_the_title() . "\n";
        echo "Profile URL: $profile_url\n";
        echo "Player URL: $player_url\n";
        echo "YouTube Channel ID: $youtube_channel_id\n";
        
        $data = mm_video_get_livestream_data($profile_url, $player_url, $youtube_channel_id);
        
        echo "\nLivestream Data:\n";
        echo "  Platform: " . ($data['platform'] ?? 'N/A') . "\n";
        echo "  Video ID: " . ($data['video_id'] ?? 'EMPTY') . "\n";
        echo "  Embed URL: " . ($data['embed_url'] ?? 'EMPTY') . "\n";
        echo "  Profile URL: " . ($data['profile_url'] ?? 'N/A') . "\n";
        
        echo "\n" . str_repeat("-", 80) . "\n\n";
    }
    wp_reset_postdata();
} else {
    echo "No livestream videos found.\n";
}

// Test the groups
echo "\n=== Testing Group Generation ===\n\n";
$groups = mm_video_get_livestream_groups();
echo "Found " . count($groups) . " groups\n\n";

foreach ($groups as $idx => $group) {
    echo "Group $idx: " . $group['owner'] . "\n";
    echo "  Items: " . count($group['items']) . "\n";
    
    foreach ($group['items'] as $item_idx => $item) {
        echo "  Item $item_idx:\n";
        echo "    Title: " . $item['title'] . "\n";
        echo "    Platform: " . $item['stream']['platform'] . "\n";
        echo "    Video ID: " . ($item['stream']['video_id'] ?? 'EMPTY') . "\n";
        echo "    Embed URL: " . (empty($item['stream']['embed_url']) ? 'EMPTY' : 'SET') . "\n";
    }
    echo "\n";
}
