import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import App from './App.vue'
import router from './router/'
import { AES_SECRET_KEY } from './config'
import { initAesEncrypt } from 'shared/utils'

// 导入需要的 Lucide 图标（用于动态组件）
import {
    MessageCircle,
    LayoutGrid,
    Workflow,
    FileText,
    Headphones,
    BarChart3,
    Users,
    User,
    Settings,
    Home,
    Ticket,
    Bot,
    ScrollText,
    Monitor,
    Server,
    ChevronDown,
    ChevronRight,
    ChevronLeft,
    PanelLeftClose,
    PanelLeftOpen,
    RefreshCw,
    Loader,
    LogOut,
    Plus,
    Trash2,
    Clock,
    Search,
    AlertTriangle,
    Wrench,
    Paperclip,
    Send,
    History,
    X,
    Menu,
    Zap
} from 'lucide-vue-next'

// Lucide 图标映射（用于全局注册）
const lucideIcons = {
    MessageCircle,
    LayoutGrid,
    Workflow,
    FileText,
    Headphones,
    BarChart3,
    Users,
    User,
    Settings,
    Home,
    Ticket,
    Bot,
    ScrollText,
    Monitor,
    Server,
    ChevronDown,
    ChevronRight,
    ChevronLeft,
    PanelLeftClose,
    PanelLeftOpen,
    RefreshCw,
    Loader,
    LogOut,
    Plus,
    Trash2,
    Clock,
    Search,
    AlertTriangle,
    Wrench,
    Paperclip,
    Send,
    History,
    X,
    Menu,
    Zap
}

// 异步初始化应用
async function initApp() {
    // 1. 初始化 AES 加密工具
    try {
        await initAesEncrypt(AES_SECRET_KEY)
        console.log('✅ AES 加密工具初始化成功')
    } catch (error) {
        console.error('❌ AES 加密工具初始化失败:', error)
    }

    // 2. 创建 Vue 应用
    const app = createApp(App)

    // 3. 注册 Pinia
    const pinia = createPinia()
    app.use(pinia)

    // 4. 注册 Element Plus
    app.use(ElementPlus)

    // 5. 注册 Lucide 图标（用于动态组件）
    for (const [name, component] of Object.entries(lucideIcons)) {
        app.component(name, component)
    }

    // 6. 注册路由
    app.use(router)

    // 7. 挂载应用
    app.mount('#app')
    
    console.log('✅ Platform 应用启动成功')
}

// 启动应用
initApp()
