#!/usr/bin/env python3
"""Check all identified faces for pose information"""

import sqlite3
import sys
import os

# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.core.config import DEFAULT_DB_PATH

def check_identified_faces(db_path: str):
    """Check all identified faces for pose information"""
    if not os.path.exists(db_path):
        print(f"Database not found: {db_path}")
        return
    
    conn = sqlite3.connect(db_path)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()
    
    # Get all identified faces with pose information
    cursor.execute('''
        SELECT 
            f.id,
            f.person_id,
            p.name || ' ' || p.last_name as person_name,
            ph.filename,
            f.pose_mode,
            f.yaw_angle,
            f.pitch_angle,
            f.roll_angle,
            f.face_confidence,
            f.quality_score,
            f.location
        FROM faces f
        JOIN people p ON f.person_id = p.id
        JOIN photos ph ON f.photo_id = ph.id
        WHERE f.person_id IS NOT NULL
        ORDER BY p.id, f.id
    ''')
    
    faces = cursor.fetchall()
    
    if not faces:
        print("No identified faces found.")
        return
    
    print(f"\n{'='*80}")
    print(f"Found {len(faces)} identified faces")
    print(f"{'='*80}\n")
    
    # Group by person
    by_person = {}
    for face in faces:
        person_id = face['person_id']
        if person_id not in by_person:
            by_person[person_id] = []
        by_person[person_id].append(face)
    
    # Print summary
    print("SUMMARY BY PERSON:")
    print("-" * 80)
    for person_id, person_faces in by_person.items():
        person_name = person_faces[0]['person_name']
        pose_modes = [f['pose_mode'] for f in person_faces]
        frontal_count = sum(1 for p in pose_modes if p == 'frontal')
        profile_count = sum(1 for p in pose_modes if 'profile' in p)
        other_count = len(pose_modes) - frontal_count - profile_count
        
        print(f"\nPerson {person_id}: {person_name}")
        print(f"  Total faces: {len(person_faces)}")
        print(f"  Frontal: {frontal_count}")
        print(f"  Profile: {profile_count}")
        print(f"  Other: {other_count}")
        print(f"  Pose modes: {set(pose_modes)}")
    
    # Print detailed information
    print(f"\n{'='*80}")
    print("DETAILED FACE INFORMATION:")
    print(f"{'='*80}\n")
    
    for face in faces:
        print(f"Face ID: {face['id']}")
        print(f"  Person: {face['person_name']} (ID: {face['person_id']})")
        print(f"  Photo: {face['filename']}")
        print(f"  Pose Mode: {face['pose_mode']}")
        print(f"  Yaw: {face['yaw_angle']:.2f}°" if face['yaw_angle'] is not None else "  Yaw: None")
        print(f"  Pitch: {face['pitch_angle']:.2f}°" if face['pitch_angle'] is not None else "  Pitch: None")
        print(f"  Roll: {face['roll_angle']:.2f}°" if face['roll_angle'] is not None else "  Roll: None")
        print(f"  Confidence: {face['face_confidence']:.3f}")
        print(f"  Quality: {face['quality_score']:.3f}")
        print(f"  Location: {face['location']}")
        print()
    
    conn.close()

if __name__ == "__main__":
    db_path = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_DB_PATH
    check_identified_faces(db_path)

