#!/usr/bin/env python3
"""
Debug YouTube scraper to see why only 3 videos are found.
"""

import os
import sys
from pathlib import Path
from dotenv import load_dotenv
import yt_dlp

# Load environment variables
load_dotenv()

def debug_youtube_channel():
    """Debug YouTube channel fetching with detailed output."""
    
    channel_url = os.getenv('YOUTUBE_CHANNEL_URL', 'https://www.youtube.com/@HVACKnowItAll')
    print(f"Testing channel: {channel_url}")
    
    # Basic options for debugging
    ydl_opts = {
        'quiet': False,  # Enable verbose output
        'extract_flat': True,  # Just get video list
        'playlistend': 50,  # Try to get 50 videos
        'ignoreerrors': True,
    }
    
    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            print("Extracting channel info...")
            channel_info = ydl.extract_info(channel_url, download=False)
            
            print(f"\nChannel info keys: {list(channel_info.keys())}")
            
            if 'entries' in channel_info:
                videos = list(channel_info['entries'])
                print(f"\n✅ Found {len(videos)} videos")
                
                # Show first few video details
                for i, video in enumerate(videos[:10]):
                    if video:
                        print(f"  {i+1}. {video.get('title', 'N/A')} (ID: {video.get('id', 'N/A')})")
                    else:
                        print(f"  {i+1}. [Empty/None video entry]")
                
                if len(videos) > 10:
                    print(f"  ... and {len(videos) - 10} more videos")
                    
            else:
                print("❌ No 'entries' key found in channel info")
                print(f"Available keys: {list(channel_info.keys())}")
                
                # Check if it's a playlist format
                if 'playlist_count' in channel_info:
                    print(f"Playlist count: {channel_info['playlist_count']}")
                
    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    debug_youtube_channel()