<?php
/**
 * Test Video ID Detection for specific YouTube video
 */

// WordPress laden
define('WP_USE_THEMES', false);
require('../../../wp-load.php');

$video_id = 'kWRhLLbLFE0';

echo "=== Testing Video ID: $video_id ===\n\n";

// 1. Check if video is live
echo "1. Checking Live Status:\n";
$live_status = mm_video_check_youtube_live_status($video_id);
echo "   Result: " . ($live_status ? "LIVE" : "NOT LIVE") . "\n\n";

// 2. Try to fetch video info from YouTube
echo "2. Fetching YouTube HTML:\n";
$url = "https://www.youtube.com/watch?v=" . $video_id;
$response = wp_remote_get($url);
if (!is_wp_error($response)) {
    $html = wp_remote_retrieve_body($response);
    
    // Check for live indicators
    if (stripos($html, '"isLiveContent":true') !== false) {
        echo "   ✓ isLiveContent found\n";
    }
    if (stripos($html, '"isLive":true') !== false) {
        echo "   ✓ isLive found\n";
    }
    if (stripos($html, '"isLiveNow":true') !== false) {
        echo "   ✓ isLiveNow found\n";
    }
    if (stripos($html, 'LIVE_NOW') !== false) {
        echo "   ✓ LIVE_NOW badge found\n";
    }
    
    // Check for DVR
    if (stripos($html, '"isLiveDvrEnabled":true') !== false) {
        echo "   ✓ DVR enabled\n";
    }
    
    echo "\n";
} else {
    echo "   ERROR: " . $response->get_error_message() . "\n\n";
}

// 3. Check DreamTripspk channel for live videos
echo "3. Checking DreamTripspk Channel:\n";
$channel_id = 'UCetYFjkhf7S7LwiuJxeC28g';
$channel_url = 'https://www.youtube.com/channel/' . $channel_id . '/live';

$extracted_id = mm_video_extract_youtube_live_video_id($channel_url);
if ($extracted_id) {
    echo "   ✓ Found video ID: $extracted_id\n";
    if ($extracted_id === $video_id) {
        echo "   ✓ MATCH! This is the current live video\n";
    } else {
        echo "   ✗ DIFFERENT video is live: $extracted_id\n";
    }
} else {
    echo "   ✗ No live video found on channel\n";
}

echo "\n";

// 4. Check cache
echo "4. Cache Status:\n";
$cache_key_video = 'mm_yt_live_v2_' . md5($channel_url);
$cached_video_id = get_transient($cache_key_video);
echo "   Video ID Cache: " . ($cached_video_id ? $cached_video_id : "EMPTY") . "\n";

$cache_key_status = 'mm_yt_status_' . $video_id;
$cached_status = get_transient($cache_key_status);
echo "   Live Status Cache: " . ($cached_status !== false ? ($cached_status ? "LIVE" : "NOT LIVE") : "EMPTY") . "\n";

echo "\n=== Test Complete ===\n";
