import type { App } from 'vue'
import type { ModulePlugin } from '@/core/types'
import { container, SERVICE_TOKENS } from '@/core/di-container'
import NostrFeed from './components/NostrFeed.vue'
import { useFeed } from './composables/useFeed'
import { FeedService } from './services/FeedService'
import { ProfileService } from './services/ProfileService'
import { ReactionService } from './services/ReactionService'

/**
 * Nostr Feed Module Plugin
 * Provides social feed functionality with admin announcements support
 */
export const nostrFeedModule: ModulePlugin = {
  name: 'nostr-feed',
  version: '1.0.0',
  dependencies: ['base'],

  async install(app: App) {
    console.log('nostr-feed module: Starting installation...')

    // Register services in DI container
    const feedService = new FeedService()
    const profileService = new ProfileService()
    const reactionService = new ReactionService()

    container.provide(SERVICE_TOKENS.FEED_SERVICE, feedService)
    container.provide(SERVICE_TOKENS.PROFILE_SERVICE, profileService)
    container.provide(SERVICE_TOKENS.REACTION_SERVICE, reactionService)
    console.log('nostr-feed module: Services registered in DI container')

    // Initialize services
    console.log('nostr-feed module: Initializing services...')
    await Promise.all([
      feedService.initialize({
        waitForDependencies: true,
        maxRetries: 3
      }),
      profileService.initialize({
        waitForDependencies: true,
        maxRetries: 3
      }),
      reactionService.initialize({
        waitForDependencies: true,
        maxRetries: 3
      })
    ])
    console.log('nostr-feed module: Services initialized')

    // Register components globally
    app.component('NostrFeed', NostrFeed)
    console.log('nostr-feed module: Installation complete')
  },

  components: {
    NostrFeed
  },

  composables: {
    useFeed
  }
}

export default nostrFeedModule