import type { App } from 'vue'
import type { ModulePlugin } from '@/core/types'
import { container, SERVICE_TOKENS } from '@/core/di-container'
import { relayHub } from './nostr/relay-hub'

// Import auth services
import { auth } from './auth/auth-service'

// Import PWA services
import { pwaService } from './pwa/pwa-service'

// Import core services
import { paymentService } from '@/core/services/PaymentService'
import { visibilityService } from '@/core/services/VisibilityService'
import { storageService } from '@/core/services/StorageService'
import { toastService } from '@/core/services/ToastService'
import { InvoiceService } from '@/core/services/invoiceService'
import { LnbitsAPI } from '@/lib/api/lnbits'

// Create service instances
const invoiceService = new InvoiceService()
const lnbitsAPI = new LnbitsAPI()

/**
 * Base Module Plugin
 * Provides core infrastructure: Nostr, Auth, PWA, and UI components
 */
export const baseModule: ModulePlugin = {
  name: 'base',
  version: '1.0.0',
  
  async install(_app: App, options?: any) {
    console.log('🔧 Installing base module...')

    // Register core Nostr services
    container.provide(SERVICE_TOKENS.RELAY_HUB, relayHub)
    
    // Register auth service
    container.provide(SERVICE_TOKENS.AUTH_SERVICE, auth)
    
    // Register payment service
    container.provide(SERVICE_TOKENS.PAYMENT_SERVICE, paymentService)
    
    // Register visibility service
    container.provide(SERVICE_TOKENS.VISIBILITY_SERVICE, visibilityService)
    
    // Register storage service
    container.provide(SERVICE_TOKENS.STORAGE_SERVICE, storageService)
    
    // Register toast service
    container.provide(SERVICE_TOKENS.TOAST_SERVICE, toastService)
    
    // Register invoice service
    container.provide(SERVICE_TOKENS.INVOICE_SERVICE, invoiceService)
    
    // Register API services
    container.provide(SERVICE_TOKENS.LNBITS_API, lnbitsAPI)
    
    // Register PWA service
    container.provide('pwaService', pwaService)

    // Initialize core services in dependency order
    relayHub.setRelayUrls(options?.config?.nostr?.relays || [])
    await relayHub.initialize()
    
    // Initialize LnbitsAPI first since AuthService depends on it
    await lnbitsAPI.initialize({
      waitForDependencies: false, // LnbitsAPI is core infrastructure with no dependencies
      maxRetries: 1
    })
    
    // Auth initialization moved to app.ts before router guards
    await paymentService.initialize({
      waitForDependencies: true, // PaymentService depends on AuthService
      maxRetries: 3
    })
    await visibilityService.initialize({
      waitForDependencies: false, // VisibilityService has no dependencies
      maxRetries: 1
    })
    await storageService.initialize({
      waitForDependencies: true, // StorageService depends on AuthService
      maxRetries: 3
    })
    await toastService.initialize({
      waitForDependencies: false, // ToastService has no dependencies
      maxRetries: 1
    })
    // InvoiceService doesn't need initialization as it's not a BaseService
    
    console.log('✅ Base module installed successfully')
  },

  async uninstall() {
    console.log('🗑️ Uninstalling base module...')
    
    // Cleanup services
    await relayHub.dispose()
    await auth.dispose()
    await paymentService.dispose()
    await visibilityService.dispose()
    await storageService.dispose()
    await toastService.dispose()
    // InvoiceService doesn't need disposal as it's not a BaseService
    await lnbitsAPI.dispose()
    
    // Remove services from DI container
    container.remove(SERVICE_TOKENS.LNBITS_API)
    container.remove(SERVICE_TOKENS.INVOICE_SERVICE)
    
    console.log('✅ Base module uninstalled')
  },

  services: {
    relayHub,
    auth,
    paymentService,
    visibilityService,
    storageService,
    toastService,
    invoiceService,
    pwaService
  },

  // No routes - base module is pure infrastructure
  routes: [],
  
  // No UI components at module level - they'll be imported as needed
  components: {}
}

export default baseModule