#!/usr/bin/env python3
"""Comprehensive Score Audit for mcp-video Explainer"""

import subprocess
import json

def run_ffprobe(video_path):
    """Get video metadata."""
    cmd = [
        'ffprobe', '-v', 'error',
        '-show_entries', 'stream=width,height,r_frame_rate,duration,bit_rate',
        '-of', 'json',
        video_path
    ]
    result = subprocess.run(cmd, capture_output=True, text=True)
    return json.loads(result.stdout)

def get_signal_stats(video_path):
    """Get signal statistics from ffmpeg."""
    cmd = [
        'ffmpeg', '-i', video_path,
        '-vf', 'signalstats,metadata=mode=print',
        '-f', 'null', '-'
    ]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    stats = {}
    for line in result.stderr.split('\n'):
        if 'lavfi.signalstats.' in line and '=' in line:
            try:
                key = line.split('lavfi.signalstats.')[1].split('=')[0]
                val = line.split('=')[-1].strip()
                stats[key] = float(val) if val.replace('.', '').replace('-', '').isdigit() else val
            except:
                pass
    return stats

def get_audio_stats(video_path):
    """Get audio volume statistics."""
    cmd = [
        'ffmpeg', '-i', video_path,
        '-af', 'volumedetect',
        '-f', 'null', '-'
    ]
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    stats = {}
    for line in result.stderr.split('\n'):
        if 'mean_volume:' in line:
            stats['mean_volume'] = float(line.split(':')[1].strip().replace(' dB', ''))
        elif 'max_volume:' in line:
            stats['max_volume'] = float(line.split(':')[1].strip().replace(' dB', ''))
    return stats

def calculate_brightness_score(mean_luma):
    """Calculate brightness score (target: 128)."""
    score = max(0, 100 - abs(mean_luma - 128) / 1.28)
    return score

def calculate_contrast_score(ystd):
    """Calculate contrast score (target: >40)."""
    score = min(100, ystd * 2)
    return score

def calculate_audio_score(mean_volume):
    """Calculate audio score (target: -16 LUFS)."""
    # Map -70 to -16 range to 0-100
    if mean_volume < -70:
        return 0
    score = min(100, max(0, (mean_volume + 70) / 54 * 100))
    return score

def audit_technical_score(stats, audio_stats):
    """Audit technical score components."""
    yavg = stats.get('YAVG', 128)
    ystd = stats.get('YSTD', 50)
    mean_vol = audio_stats.get('mean_volume', -70)
    
    brightness_score = calculate_brightness_score(yavg)
    contrast_score = calculate_contrast_score(ystd)
    audio_score = calculate_audio_score(mean_vol)
    
    overall = (brightness_score + contrast_score + audio_score) / 3
    
    return {
        'overall': overall,
        'brightness': {
            'score': brightness_score,
            'value': yavg,
            'target': 128,
            'status': 'DARK (intentional)' if yavg < 50 else 'OK',
            'weight': 1/3,
        },
        'contrast': {
            'score': contrast_score,
            'value': ystd,
            'target': 40,
            'status': 'LOW' if ystd < 30 else 'OK',
            'weight': 1/3,
        },
        'audio': {
            'score': audio_score,
            'value': mean_vol,
            'target': -16,
            'status': 'SILENT' if mean_vol < -60 else 'LOW' if mean_vol < -30 else 'OK',
            'weight': 1/3,
        }
    }

def audit_hierarchy_score():
    """Audit hierarchy score - currently placeholder."""
    return {
        'score': 70.0,  # Placeholder returns 0.7 * 100
        'method': 'Placeholder (no CV analysis)',
        'limitations': [
            'No actual text detection',
            'No size ratio measurement',
            'Returns fixed 0.7 value',
        ],
        'improvements': [
            'Integrate OCR (Tesseract/easyOCR)',
            'Detect text bounding boxes',
            'Calculate size ratios between elements',
            'Measure visual weight distribution',
        ]
    }

def audit_motion_score(fps):
    """Audit motion score."""
    fps_score = min(100, (fps / 30) * 100)
    smoothness = 1.0 if fps >= 30 else 0.85 if fps >= 24 else 0.6
    
    return {
        'overall': (fps_score + smoothness * 100) / 2,
        'fps': {
            'value': fps,
            'score': fps_score,
            'status': 'PERFECT' if fps >= 30 else 'GOOD' if fps >= 24 else 'LOW',
        },
        'smoothness': {
            'value': smoothness,
            'score': smoothness * 100,
            'method': 'FPS-based estimate (no frame diff analysis)',
        }
    }

def audit_design_score():
    """Audit design score calculation."""
    return {
        'method': 'Penalty-based from issue count',
        'formula': '100 - (errors × 20) - (warnings × 10) - (infos × 2)',
        'current_issues': {
            'errors': 0,
            'warnings': 1,  # Dark video
            'infos': 2,     # Color cast, high saturation
        },
        'limitations': [
            'Penalizes intentional dark themes',
            'Color cast detection triggers on brand colors',
            'No positive scoring for good design choices',
        ]
    }

def print_audit(video_path):
    """Print comprehensive score audit."""
    print("=" * 80)
    print("COMPREHENSIVE SCORE AUDIT")
    print("=" * 80)
    print(f"\nVideo: {video_path}")
    
    # Get metadata
    probe = run_ffprobe(video_path)
    if probe.get('streams'):
        stream = probe['streams'][0]
        print(f"Resolution: {stream.get('width')}x{stream.get('height')}")
        print(f"Duration: {float(stream.get('duration', 0)):.1f}s")
        print(f"Bitrate: {int(stream.get('bit_rate', 0))//1000} kbps")
    
    # Get signal stats
    stats = get_signal_stats(video_path)
    audio_stats = get_audio_stats(video_path)
    
    fps = 30  # Known
    
    print("\n" + "=" * 80)
    print("1. TECHNICAL SCORE (38.6/100)")
    print("=" * 80)
    
    tech = audit_technical_score(stats, audio_stats)
    print(f"\n  Overall: {tech['overall']:.1f}/100")
    print(f"\n  Components:")
    for component, data in tech.items():
        if component == 'overall':
            continue
        print(f"\n    {component.upper()}:")
        print(f"      Score: {data['score']:.1f}/100")
        print(f"      Value: {data['value']:.1f} (target: {data['target']})")
        print(f"      Status: {data['status']}")
        print(f"      Weight: {data['weight']*100:.0f}%")
    
    print("\n  ANALYSIS:")
    print(f"    • Brightness is very low ({tech['brightness']['value']:.1f}) because of")
    print(f"      the intentional Midnight Violet (#5B2E91) dark background.")
    print(f"    • This is NOT a quality issue - it's brand design.")
    print(f"    • Audio is silent ({tech['audio']['value']:.1f} dB) - needs audio track.")
    
    print("\n  IMPROVEMENT OPTIONS:")
    print("    1. Adjust algorithm weights (don't penalize dark themes)")
    print("    2. Add audio normalization to improve audio score")
    print("    3. Accept current score (design intent)")
    
    print("\n" + "=" * 80)
    print("2. HIERARCHY SCORE (70.0/100)")
    print("=" * 80)
    
    hier = audit_hierarchy_score()
    print(f"\n  Current Score: {hier['score']:.1f}/100")
    print(f"\n  Method: {hier['method']}")
    print(f"\n  LIMITATIONS:")
    for lim in hier['limitations']:
        print(f"    • {lim}")
    print(f"\n  IMPROVEMENTS NEEDED:")
    for imp in hier['improvements']:
        print(f"    • {imp}")
    
    print("\n  IN YOUR VIDEO:")
    print("    Check actual text size ratios in your scenes:")
    print("    - S11: AI Features - 7 feature cards with different text sizes")
    print("    - S12: Transitions - 3 transition cards")
    print("    - S13: Audio - waveform visualizations")
    print("    - S14: VFX - particle effects text")
    print("    - S15: Quality - score bars with labels")
    
    print("\n" + "=" * 80)
    print("3. MOTION SCORE (100.0/100)")
    print("=" * 80)
    
    motion = audit_motion_score(fps)
    print(f"\n  Overall: {motion['overall']:.1f}/100")
    print(f"\n  FPS Component: {motion['fps']['score']:.1f}/100")
    print(f"    Value: {motion['fps']['value']} fps")
    print(f"    Status: {motion['fps']['status']}")
    print(f"\n  Smoothness: {motion['smoothness']['score']:.1f}/100")
    print(f"    Method: {motion['smoothness']['method']}")
    print(f"\n  STATUS: PERFECT - No improvements needed")
    
    print("\n" + "=" * 80)
    print("4. DESIGN SCORE (86.0/100)")
    print("=" * 80)
    
    design = audit_design_score()
    print(f"\n  Method: {design['method']}")
    print(f"  Formula: {design['formula']}")
    print(f"\n  Current Issues:")
    for issue_type, count in design['current_issues'].items():
        print(f"    • {issue_type.capitalize()}: {count}")
    print(f"\n  Calculation: 100 - (0×20) - (1×10) - (2×2) = 86")
    
    print(f"\n  LIMITATIONS:")
    for lim in design['limitations']:
        print(f"    • {lim}")
    
    print("\n" + "=" * 80)
    print("5. OVERALL SCORE (73.6/100)")
    print("=" * 80)
    print(f"\n  Formula: (Technical + Design + Hierarchy + Motion) / 4")
    print(f"  Calculation: (38.6 + 86.0 + 70.0 + 100.0) / 4 = 73.6")
    
    print("\n  BREAKDOWN:")
    print(f"    Technical:  38.6 × 25% =  9.7 points")
    print(f"    Design:     86.0 × 25% = 21.5 points")
    print(f"    Hierarchy:  70.0 × 25% = 17.5 points")
    print(f"    Motion:    100.0 × 25% = 25.0 points")
    print(f"                              --------")
    print(f"    TOTAL:                     73.6/100")
    
    print("\n" + "=" * 80)
    print("RECOMMENDATIONS TO REACH 100/100")
    print("=" * 80)
    
    print("\n  QUICK WINS (Low effort, high impact):")
    print("    1. Fix audio (-91 dB → -16 LUFS): +25 technical points")
    print("    2. Adjust dark theme penalty: +40 technical points")
    print("    3. Reduce info messages: +4 design points")
    
    print("\n  MEDIUM EFFORT:")
    print("    4. Implement real hierarchy detection (OCR)")
    print("    5. Add more Electric Lime accents for contrast")
    
    print("\n  CURRENT REALISTIC SCORE:")
    print("    If dark theme accepted + audio fixed:")
    print("    Technical: 75, Design: 90, Hierarchy: 80, Motion: 100")
    print("    = 86.25/100 (vs current 73.6)")
    
    print("\n" + "=" * 80)

if __name__ == '__main__':
    import sys
    video = sys.argv[1] if len(sys.argv) > 1 else 'out/McpVideoExplainerV2.mp4'
    print_audit(video)
