import type { App } from 'vue'
import type { ModulePlugin } from '@/core/types'
import { container, SERVICE_TOKENS } from '@/core/di-container'
import WalletService from './services/WalletService'
import { WalletWebSocketService } from './services/WalletWebSocketService'
import { WalletPage, SendDialog, ReceiveDialog, WalletTransactions } from './components'

export const walletModule: ModulePlugin = {
  name: 'wallet',
  version: '1.0.0',
  dependencies: ['base'],
  
  async install(app: App) {
    // Create service instances
    const walletService = new WalletService()
    const walletWebSocketService = new WalletWebSocketService()

    // Register services in DI container BEFORE initialization
    container.provide(SERVICE_TOKENS.WALLET_SERVICE, walletService)
    container.provide(SERVICE_TOKENS.WALLET_WEBSOCKET_SERVICE, walletWebSocketService)

    // Initialize services with proper dependency waiting
    await walletService.initialize({
      waitForDependencies: true,
      maxRetries: 3
    })

    // Initialize WebSocket service after wallet service
    await walletWebSocketService.initialize({
      waitForDependencies: true,
      maxRetries: 3
    })

    // Register components globally AFTER services are initialized
    app.component('WalletPage', WalletPage)
    app.component('SendDialog', SendDialog)
    app.component('ReceiveDialog', ReceiveDialog)
    app.component('WalletTransactions', WalletTransactions)
  },
  
  routes: [
    {
      path: '/wallet',
      name: 'Wallet',
      component: () => import('./views/WalletPage.vue'),
      meta: { requiresAuth: true }
    }
  ],
  
  components: {
    WalletPage,
    SendDialog,
    ReceiveDialog,
    WalletTransactions
  }
}

export default walletModule