"""
Main application entry point for the Cluster4NPU UI application.

This module initializes the PyQt5 application, applies the theme, and launches
the main dashboard window. It serves as the primary entry point for the
modularized UI application.

Main Components:
    - Application initialization and configuration
    - Theme application and font setup
    - Main window instantiation and display
    - Application event loop management

Usage:
    python -m cluster4npu_ui.main
    
    # Or directly:
    from cluster4npu_ui.main import main
    main()
"""

import sys
import os
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt

# Add the parent directory to the path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from cluster4npu_ui.config.theme import apply_theme
from cluster4npu_ui.ui.windows.login import DashboardLogin


def setup_application():
    """Initialize and configure the QApplication."""
    # Enable high DPI support BEFORE creating QApplication
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
    
    # Create QApplication if it doesn't exist
    if not QApplication.instance():
        app = QApplication(sys.argv)
    else:
        app = QApplication.instance()
    
    # Set application properties
    app.setApplicationName("Cluster4NPU")
    app.setApplicationVersion("1.0.0")
    app.setOrganizationName("Cluster4NPU Team")
    
    # Set application font
    app.setFont(QFont("Arial", 9))
    
    # Apply the harmonious theme
    apply_theme(app)
    
    return app


def main():
    """Main application entry point."""
    try:
        # Setup the application
        app = setup_application()
        
        # Create and show the main dashboard login window
        dashboard = DashboardLogin()
        dashboard.show()
        
        # Start the application event 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()