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

echo "=== Debug: Livestream Filtering ===\n\n";

$args = array(
    'post_type' => 'mm_livestream',
    'posts_per_page' => -1,
    'post_status' => 'publish',
);

$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, '_mm_livestream_url', true);
        $player_url = get_post_meta($post_id, '_mm_livestream_player_url', true);
        $youtube_channel_id = get_post_meta($post_id, '_mm_livestream_youtube_channel_id', true);
        $owner = get_post_meta($post_id, '_mm_livestream_owner', true);
        
        echo "Post #$post_id: " . get_the_title() . "\n";
        echo "  Owner: $owner\n";
        echo "  Profile: $profile_url\n";
        echo "  Player: $player_url\n";
        echo "  Channel ID: $youtube_channel_id\n";
        
        $stream = mm_video_get_livestream_data($profile_url, $player_url, $youtube_channel_id);
        
        echo "  Platform: " . $stream['platform'] . "\n";
        echo "  Video ID: " . ($stream['video_id'] ?? 'EMPTY') . "\n";
        echo "  Embed URL: " . ($stream['embed_url'] ?? 'EMPTY') . "\n";
        
        if ($stream['platform'] === 'youtube') {
            $video_id = $stream['video_id'] ?? '';
            if ($video_id) {
                $is_live = mm_video_check_youtube_live_status($video_id);
                echo "  Live Status Check: " . ($is_live ? 'LIVE ✓' : 'NOT LIVE ✗') . "\n";
            } else {
                echo "  Live Status Check: SKIPPED (no video_id)\n";
            }
        }
        
        echo "\n";
    }
    wp_reset_postdata();
} else {
    echo "No livestream posts found!\n";
}
