"""
PyQt5前端主程序入口
"""
import sys
from PyQt5.QtWidgets import QApplication, QMessageBox
from frontend.main_window import MainWindow
from frontend.utils.api_client import APIClient


def check_backend_connection():
    """检查后端连接"""
    try:
        client = APIClient()
        client._get("/health")
        return True
    except Exception as e:
        return False


def main():
    """主函数"""
    app = QApplication(sys.argv)

    # 检查后端连接
    if not check_backend_connection():
        QMessageBox.critical(None, "连接失败", "无法连接到后端服务器！\n"
                             "请确保后端服务已启动（运行 python backend/main.py）\n"
                             "错误信息：后端服务未运行或无法访问")
        sys.exit(1)

    # 创建主窗口
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
