"""
Eden's Video Learning System - Fixed for all formats
"""
import yt_dlp
import whisper
import cv2
import subprocess
import os
import json
from pathlib import Path

class EdenVideoLearner:
    def __init__(self):
        self.download_dir = "/Eden/LEARNING/videos"
        self.transcripts_dir = "/Eden/LEARNING/transcripts"
        self.analysis_dir = "/Eden/LEARNING/analysis"
        
        Path(self.download_dir).mkdir(parents=True, exist_ok=True)
        Path(self.transcripts_dir).mkdir(parents=True, exist_ok=True)
        Path(self.analysis_dir).mkdir(parents=True, exist_ok=True)
        
        print("🧠 Loading Whisper AI...")
        self.whisper_model = whisper.load_model("base")
        print("✅ Whisper loaded")
        
    def download_video(self, url, title=None):
        """Download video in H.264 format for compatibility"""
        try:
            if not title:
                title = "video"
            safe_title = "".join(c for c in title if c.isalnum() or c in (' ', '-', '_')).rstrip()
            safe_title = safe_title[:50]
            
            output_path = f"{self.download_dir}/{safe_title}.mp4"
            
            print(f"📥 Eden downloading: {title}...")
            
            ydl_opts = {
                'format': 'bestvideo[ext=mp4][height<=720]+bestaudio[ext=m4a]/best[ext=mp4][height<=720]',
                'outtmpl': output_path,
                'merge_output_format': 'mp4',
                'postprocessors': [{
                    'key': 'FFmpegVideoConvertor',
                    'preferedformat': 'mp4',
                }],
                'quiet': True,
            }
            
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=True)
                
            print(f"   ✅ Downloaded: {output_path}")
            return output_path, info
            
        except Exception as e:
            print(f"   ❌ Download failed: {e}")
            return None, None
    
    def transcribe_audio(self, video_file):
        """Transcribe audio from video"""
        try:
            print(f"🔊 Eden listening to audio...")
            result = self.whisper_model.transcribe(video_file)
            transcript = result['text']
            
            transcript_file = video_file.replace(self.download_dir, self.transcripts_dir) + ".txt"
            Path(transcript_file).parent.mkdir(parents=True, exist_ok=True)
            
            with open(transcript_file, 'w') as f:
                f.write(transcript)
            
            print(f"   ✅ Transcribed {len(transcript)} characters")
            return transcript, transcript_file
            
        except Exception as e:
            print(f"   ❌ Transcription failed: {e}")
            return None, None
    
    def analyze_frames_ffmpeg(self, video_file, sample_rate=30):
        """Analyze video frames using ffmpeg + OpenCV"""
        try:
            print(f"👁️ Eden watching video (using ffmpeg)...")
            
            # Get video info
            cmd = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', video_file]
            result = subprocess.run(cmd, capture_output=True, text=True)
            info = json.loads(result.stdout)
            
            # Find video stream
            video_stream = next((s for s in info['streams'] if s['codec_type'] == 'video'), None)
            if not video_stream:
                print("   ❌ No video stream found")
                return []
            
            duration = float(info['format']['duration'])
            fps = eval(video_stream['r_frame_rate'])  # e.g., "30/1" -> 30.0
            total_frames = int(duration * fps)
            
            # Extract frames at intervals
            frame_times = []
            current_time = 0
            while current_time < duration:
                frame_times.append(current_time)
                current_time += sample_rate
            
            sampled_frames = []
            for timestamp in frame_times[:10]:  # Limit to 10 samples for speed
                # Extract frame at this timestamp
                cmd = [
                    'ffmpeg', '-ss', str(timestamp), '-i', video_file,
                    '-vframes', '1', '-f', 'image2pipe', '-pix_fmt', 'rgb24',
                    '-vcodec', 'rawvideo', '-'
                ]
                result = subprocess.run(cmd, capture_output=True, stderr=subprocess.DEVNULL)
                
                if result.returncode == 0 and len(result.stdout) > 0:
                    sampled_frames.append({
                        'timestamp': timestamp,
                        'has_frame': True
                    })
            
            print(f"   ✅ Analyzed {len(sampled_frames)} key frames from ~{total_frames} total")
            return sampled_frames
            
        except Exception as e:
            print(f"   ⚠️ Frame analysis failed: {e}")
            print(f"   (Eden can still learn from audio)")
            return []
    
    def learn_from_video(self, url, title=None):
        """Complete learning pipeline"""
        print(f"\n{'='*70}")
        print(f"🎓 EDEN LEARNING FROM VIDEO")
        print(f"{'='*70}\n")
        
        video_file, info = self.download_video(url, title)
        if not video_file:
            return None
        
        transcript, transcript_file = self.transcribe_audio(video_file)
        frames = self.analyze_frames_ffmpeg(video_file, sample_rate=30)
        
        learning = {
            'title': title or info.get('title', 'Unknown'),
            'url': url,
            'duration': info.get('duration', 0),
            'video_file': video_file,
            'transcript': transcript,
            'transcript_file': transcript_file,
            'frames_analyzed': len(frames),
            'key_frames': frames,
        }
        
        analysis_file = video_file.replace(self.download_dir, self.analysis_dir) + "_analysis.json"
        Path(analysis_file).parent.mkdir(parents=True, exist_ok=True)
        
        with open(analysis_file, 'w') as f:
            json.dump(learning, f, indent=2)
        
        print(f"\n{'='*70}")
        print(f"✅ LEARNING COMPLETE")
        print(f"{'='*70}")
        print(f"Video: {learning['title']}")
        print(f"Duration: {learning['duration']}s ({learning['duration']//60} min)")
        print(f"Transcript: {len(transcript) if transcript else 0} chars")
        print(f"Frames: {learning['frames_analyzed']} analyzed")
        print(f"{'='*70}\n")
        
        return learning

eden_video_learner = EdenVideoLearner()
