import analytics from '../utils/posthog/index.js'
import pageTracker from '../utils/posthog/page-tracker'
import readingTracker from '../utils/posthog/reading-tracker'
import searchTracker from '../utils/posthog/search-tracker.js'
import userTracker from '../utils/posthog/user-tracker'
import errorTracker from '../utils/posthog/error-tracker'
import performanceTracker from '../utils/posthog/performance-tracker'
import config from '@/src/config/posthog.config.js'

export default {
  install(app, options = {}) {
    // 合并配置
    const finalConfig = {
      ...config,
      ...options,
    }

    // 初始化SDK
    analytics.init(finalConfig)

    // 初始化错误追踪
    errorTracker.init()

    // 注入全局属性
    app.config.globalProperties.$analytics = analytics
    app.config.globalProperties.$pageTracker = pageTracker
    app.config.globalProperties.$readingTracker = readingTracker
    app.config.globalProperties.$searchTracker = searchTracker
    app.config.globalProperties.$userTracker = userTracker
    app.config.globalProperties.$errorTracker = errorTracker
    app.config.globalProperties.$performanceTracker = performanceTracker

    // 提供依赖注入
    app.provide('analytics', analytics)
    app.provide('pageTracker', pageTracker)
    app.provide('readingTracker', readingTracker)
    app.provide('searchTracker', searchTracker)
    app.provide('userTracker', userTracker)
    app.provide('errorTracker', errorTracker)
    app.provide('performanceTracker', performanceTracker)

    console.log('[Analytics Plugin] Installed')
  },
}