#!/usr/bin/env python3
"""
Use browser cookies for YouTube authentication
"""

import yt_dlp
from pathlib import Path

def test_with_browser_cookies():
    """Test YouTube access using browser cookies"""
    
    print("🌐 Attempting to use browser cookies...")
    print("=" * 60)
    
    # Try different browser options
    browsers = ['firefox', 'chrome', 'chromium', 'edge', 'safari']
    
    for browser in browsers:
        print(f"\nTrying {browser}...")
        
        ydl_opts = {
            'cookiesfrombrowser': (browser,),  # Use cookies from browser
            'quiet': False,
            'no_warnings': False,
            'extract_flat': False,
            'skip_download': True,
            'writesubtitles': True,
            'writeautomaticsub': True,
            'subtitleslangs': ['en'],
        }
        
        try:
            test_video = "https://www.youtube.com/watch?v=TpdYT_itu9U"
            
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(test_video, download=False)
                
                if info:
                    print(f"✅ Success with {browser}!")
                    print(f"Video: {info.get('title', 'Unknown')}")
                    
                    # Check transcripts
                    subtitles = info.get('subtitles', {})
                    auto_captions = info.get('automatic_captions', {})
                    
                    if 'en' in subtitles or 'en' in auto_captions:
                        print(f"✅ Transcripts available!")
                        
                        # Now save the cookies for future use
                        cookie_file = Path("data_production_backlog/.cookies/youtube_browser.txt")
                        ydl_opts_save = {
                            'cookiesfrombrowser': (browser,),
                            'cookiefile': str(cookie_file),
                            'quiet': True,
                        }
                        
                        with yt_dlp.YoutubeDL(ydl_opts_save) as ydl2:
                            ydl2.extract_info(test_video, download=False)
                        
                        if cookie_file.exists():
                            lines = len(cookie_file.read_text().splitlines())
                            print(f"📄 Cookies saved: {lines} lines")
                    
                    return browser
                    
        except Exception as e:
            error_msg = str(e)
            if "browser is not installed" in error_msg.lower():
                print(f"  ❌ {browser} not found")
            elif "no profile" in error_msg.lower():
                print(f"  ❌ No {browser} profile found")
            elif "could not extract" in error_msg.lower():
                print(f"  ❌ Could not extract cookies from {browser}")
            else:
                print(f"  ❌ Error: {error_msg[:100]}")
    
    print("\n❌ No browser cookies available")
    print("\nTo fix this:")
    print("1. Open Firefox or Chrome")
    print("2. Log into YouTube with benreed1987@gmail.com")
    print("3. Make sure you're logged in and can watch videos")
    print("4. Keep the browser open and run this script again")
    
    return None

if __name__ == "__main__":
    browser = test_with_browser_cookies()
    
    if browser:
        print(f"\n✅ Successfully authenticated using {browser} cookies!")
        print("Transcripts can now be fetched.")
    else:
        print("\n⚠️ Manual cookie export required:")
        print("1. Install 'Get cookies.txt LOCALLY' extension")
        print("2. Log into YouTube")
        print("3. Export cookies while on youtube.com")
        print("4. Save as: data_production_backlog/.cookies/youtube_manual.txt")