#!/usr/bin/env python3
"""
Quick backfill of face recognition scans for existing files
This scans all media files currently in /opt/immich/md and logs results to database
"""

import os
import sys
from pathlib import Path

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Bootstrap PostgreSQL adapter before any database imports
from modules.db_bootstrap import bootstrap_database
bootstrap_database()

from modules.face_recognition_module import FaceRecognitionModule
from modules.unified_database import UnifiedDatabase
from modules.settings_manager import SettingsManager

# Configuration
SCAN_BASE_DIR = "/opt/immich/md"
DATABASE_PATH = "/opt/media-downloader/database/media_downloader.db"

# Supported file extensions
IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.heic'}
VIDEO_EXTENSIONS = {'.mp4', '.mov', '.avi', '.mkv', '.webm', '.flv', '.m4v'}
SUPPORTED_EXTENSIONS = IMAGE_EXTENSIONS | VIDEO_EXTENSIONS


def main():
    print("🔄 Quick Face Recognition Backfill")
    print("=" * 70)

    db = UnifiedDatabase()
    settings_manager = SettingsManager(DATABASE_PATH)
    face_module = FaceRecognitionModule(unified_db=db)

    # Get settings
    settings = settings_manager.get('face_recognition', {})
    if not settings.get('enabled', False):
        print("✗ Face recognition is disabled in settings")
        sys.exit(1)

    tolerance = settings.get('tolerance', 0.6)
    print(f"Scanning: {SCAN_BASE_DIR}")
    print(f"Tolerance: {tolerance}")
    print("=" * 70)

    stats = {'total': 0, 'matched': 0, 'no_match': 0, 'errors': 0, 'already_scanned': 0}

    # Walk through all files
    for root, dirs, files in os.walk(SCAN_BASE_DIR):
        for filename in files:
            file_path = os.path.join(root, filename)
            file_ext = os.path.splitext(filename)[1].lower()

            if file_ext not in SUPPORTED_EXTENSIONS:
                continue

            stats['total'] += 1

            # Check if already scanned
            existing = db.get_face_recognition_result(file_path)
            if existing:
                stats['already_scanned'] += 1
                if stats['total'] % 50 == 0:
                    print(f"Progress: {stats['total']} files processed, {stats['already_scanned']} already scanned, {stats['matched']} newly matched...")
                continue

            try:
                # Scan the file
                is_video = file_ext in VIDEO_EXTENSIONS
                result = face_module.check_image(file_path, tolerance=tolerance, is_video=is_video)

                # Log to database
                db.log_face_recognition_scan(
                    file_path=file_path,
                    has_match=result.get('has_match', False),
                    matched_person=result.get('person_name'),
                    confidence=result.get('confidence'),
                    face_count=result.get('face_count', 0),
                    scan_type='quick_backfill'
                )

                if result.get('has_match'):
                    stats['matched'] += 1
                    person = result.get('person_name', 'Unknown')
                    conf = result.get('confidence', 0)
                    print(f"✓ [{stats['total']}] {filename[:60]} - MATCHED: {person} ({conf:.1%})")
                else:
                    stats['no_match'] += 1

            except Exception as e:
                stats['errors'] += 1
                print(f"✗ [{stats['total']}] {filename[:60]} - ERROR: {e}")

    print("\n" + "=" * 70)
    print("📊 BACKFILL COMPLETE")
    print("=" * 70)
    print(f"Total files:       {stats['total']}")
    print(f"Already scanned:   {stats['already_scanned']}")
    print(f"Newly matched:     {stats['matched']}")
    print(f"No match:          {stats['no_match']}")
    print(f"Errors:            {stats['errors']}")
    print("=" * 70)

    db.close()


if __name__ == '__main__':
    main()
