import { createApp } from 'vue'
import { createPinia } from 'pinia'
import * as Sentry from '@sentry/vue'
import { BrowserTracing } from '@sentry/tracing'
import App from './App.vue'
import router from './router'
import { createI18n } from 'vue-i18n'
import enMessages from './i18n/en.json'
import deMessages from './i18n/de.json'
import frMessages from './i18n/fr.json'
import esMessages from './i18n/es.json'
import nlMessages from './i18n/nl.json'
import './assets/base.css'
import { api, globalAxios } from '@/services/api'
import { useAuthStore } from '@/stores/auth'
import { initAdvancedPerformanceOptimizations } from '@/utils/advancedPerformance'


const i18n = createI18n({
  legacy: false,
  locale: 'en',
  fallbackLocale: 'en',
  messages: {
    en: enMessages,
    de: deMessages,
    fr: frMessages,
    es: esMessages,
    nl: nlMessages,
  },
})

const app = createApp(App)
const pinia = createPinia()
app.use(pinia)

Sentry.init({
  app,
  dsn: import.meta.env.VITE_SENTRY_DSN,
  integrations: [
    new BrowserTracing({
      routingInstrumentation: Sentry.vueRouterInstrumentation(router),
      tracingOrigins: ['localhost', /^\//],
    }),
  ],
  tracesSampleRate: 1.0,
  environment: import.meta.env.MODE,
})

const authStore = useAuthStore()
if (authStore.accessToken) {
  authStore.fetchCurrentUser().catch((error) => {
    console.error('Failed to initialize current user state:', error)
  })
}

app.use(router)
app.use(i18n)

app.config.globalProperties.$api = api
app.config.globalProperties.$axios = globalAxios

// Initialize performance optimizations
initAdvancedPerformanceOptimizations()

app.mount('#app')
