from PySide6.QtWidgets import QApplication
import json
import os
import sys
import time
import traceback
from pathlib import Path
print("=== DEBUG INFO ===")
print(f"Python executable: {sys.executable}")
print(f"Current working dir: {os.getcwd()}")
print(f"Script location: {os.path.dirname(os.path.abspath(__file__))}")
print(f"sys.path: {sys.path[:3]}...")  # First 3 paths
print("=== STARTING APP ===")

def main():
    # Create application instance first
    app = QApplication.instance() or QApplication(sys.argv)
    
    # Show splash screen if available
    splash = None
    try:
        from splash import show_splash
        result = show_splash(app)
        if result:
            splash, app = result
            if splash is None:
                print("No splash image found, continuing without splash")
    except Exception as e:
        print(f"Could not show splash screen: {e}")

    # Add a short delay to show the splash screen
    if splash:
        print("[DEBUG] Splash screen shown, sleeping for 0.2s (reduced)")
        time.sleep(0.2)

    try:
        # Load standard MainWindow
        from ui.main_window import MainWindow
        print("✅ Using standard MainWindow")
    except Exception as e:
        print(f"❌ Could not load MainWindow: {e}")
        sys.exit(1)

    try:
        print("[DEBUG] Instantiating MainWindow...")
        # Initialize main window
        window = MainWindow()
        print("[DEBUG] MainWindow instantiated.")
        # Close splash if it exists
        if splash:
            print("[DEBUG] Closing splash screen.")
            splash.finish(window)
        # Show main window
        print("[DEBUG] Showing main window.")
        window.show()
        # Start application event loop
        print("[DEBUG] Entering app.exec() loop.")
        sys.exit(app.exec())
    except Exception as e:
        print(f"❌ Error starting application: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main()
