#!/usr/bin/env python3
"""
Launcher script for PunimTag Dashboard
Adds project root to Python path and launches the dashboard
"""

import os
import sys
import warnings
from pathlib import Path

# Suppress TensorFlow warnings (must be before DeepFace import)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
warnings.filterwarnings('ignore')

# Add project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

# Now import required modules
from src.gui.dashboard_gui import DashboardGUI
from src.gui.gui_core import GUICore
from src.core.database import DatabaseManager
from src.core.face_processing import FaceProcessor
from src.core.photo_management import PhotoManager
from src.core.tag_management import TagManager
from src.core.search_stats import SearchStats
from src.core.config import DEFAULT_DB_PATH

if __name__ == "__main__":
    # Initialize all required components
    gui_core = GUICore()
    db_manager = DatabaseManager(DEFAULT_DB_PATH, verbose=0)
    # Initialize face_processor without detector/model (will be updated by GUI)
    face_processor = FaceProcessor(db_manager, verbose=0)
    photo_manager = PhotoManager(db_manager, verbose=0)
    tag_manager = TagManager(db_manager, verbose=0)
    search_stats = SearchStats(db_manager)
    
    # Define callback functions for scan and process operations
    def on_scan(folder, recursive):
        """Callback for scanning photos"""
        return photo_manager.scan_folder(folder, recursive)
    
    def on_process(limit=None, stop_event=None, progress_callback=None, 
                   detector_backend=None, model_name=None):
        """Callback for processing faces with DeepFace settings"""
        # Update face_processor settings if provided
        if detector_backend:
            face_processor.detector_backend = detector_backend
        if model_name:
            face_processor.model_name = model_name
            
        return face_processor.process_faces(
            limit=limit,  # Pass None if no limit is specified
            stop_event=stop_event,
            progress_callback=progress_callback
        )
    
    # Create and run dashboard
    app = DashboardGUI(
        gui_core=gui_core,
        db_manager=db_manager,
        face_processor=face_processor,
        on_scan=on_scan,
        on_process=on_process,
        search_stats=search_stats,
        tag_manager=tag_manager
    )
    app.open()

