#!/usr/bin/env python3
"""
Migration script to prepare database for DeepFace
Drops all existing tables and recreates with new schema

⚠️  WARNING: This will delete ALL existing data!
Run this script before migrating to DeepFace.
"""

import sqlite3
import sys
from pathlib import Path

# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.core.database import DatabaseManager
from src.core.config import DEFAULT_DB_PATH


def migrate_database():
    """Drop all tables and reinitialize with DeepFace schema"""
    print("=" * 70)
    print("DeepFace Migration Script - Database Reset")
    print("=" * 70)
    print()
    print("⚠️  WARNING: This will delete ALL existing data!")
    print()
    print("This includes:")
    print("  • All photos")
    print("  • All faces and face encodings")
    print("  • All people and person data")
    print("  • All tags and photo-tag linkages")
    print()
    print("The database will be recreated with the new DeepFace schema.")
    print()
    
    response = input("Type 'DELETE ALL DATA' to confirm (or anything else to cancel): ")
    
    if response != "DELETE ALL DATA":
        print()
        print("❌ Migration cancelled.")
        print()
        return False
    
    print()
    print("🗑️  Dropping all existing tables...")
    print()
    
    try:
        # Connect directly to database
        conn = sqlite3.connect(DEFAULT_DB_PATH)
        cursor = conn.cursor()
        
        # Get list of all tables (excluding SQLite system tables)
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
        tables = [row[0] for row in cursor.fetchall()]
        
        if not tables:
            print("   No tables found in database.")
        else:
            # Drop all tables in correct order (respecting foreign keys)
            drop_order = ['phototaglinkage', 'person_encodings', 'faces', 'tags', 'people', 'photos']
            
            for table in drop_order:
                if table in tables:
                    cursor.execute(f'DROP TABLE IF EXISTS {table}')
                    print(f"   ✓ Dropped table: {table}")
            
            # Drop any remaining tables not in our list (excluding SQLite system tables)
            for table in tables:
                if table not in drop_order and not table.startswith('sqlite_'):
                    cursor.execute(f'DROP TABLE IF EXISTS {table}')
                    print(f"   ✓ Dropped table: {table}")
        
        conn.commit()
        conn.close()
        
        print()
        print("✅ All tables dropped successfully")
        print()
        print("🔄 Reinitializing database with DeepFace schema...")
        print()
        
        # Reinitialize with new schema
        db = DatabaseManager(DEFAULT_DB_PATH, verbose=1)
        
        print()
        print("=" * 70)
        print("✅ Database migration complete!")
        print("=" * 70)
        print()
        print("Next steps:")
        print("  1. Add photos using the dashboard (File → Add Photos)")
        print("  2. Process faces with DeepFace (Tools → Process Faces)")
        print("  3. Identify people in the Identify panel")
        print()
        print("New DeepFace features:")
        print("  • 512-dimensional face encodings (vs 128)")
        print("  • Multiple detector backends (RetinaFace, MTCNN, etc.)")
        print("  • ArcFace model for improved accuracy")
        print("  • Face confidence scores from detector")
        print()
        
        return True
        
    except Exception as e:
        print()
        print(f"❌ Error during migration: {e}")
        print()
        return False


if __name__ == "__main__":
    success = migrate_database()
    sys.exit(0 if success else 1)

