import { createApp } from 'vue'
import { createPinia } from 'pinia'
import PrimeVue from 'primevue/config'
import Aura from '@primevue/themes/aura'
import { VueQueryPlugin } from '@tanstack/vue-query'
import 'primeicons/primeicons.css'

import App from './App.vue'
import { setReservationHotelIdProvider } from './config/reservationConfig'
import { i18n } from './i18n'
import { router } from './router'
import { setUnauthorizedHandler } from './services/httpClient'
import { useAuthStore } from './stores/authStore'
import './styles/main.css'

const app = createApp(App)
const pinia = createPinia()
const authStore = useAuthStore(pinia)

setReservationHotelIdProvider(() => authStore.selectedHotelId)
setUnauthorizedHandler(() => {
  const currentPath = router.currentRoute.value.fullPath
  authStore.clearAuth()
  if (!currentPath.startsWith('/login')) {
    void router.push({
      path: '/login',
      query: {
        redirect: currentPath,
      },
    })
  }
})

app.use(pinia)
app.use(router)
app.use(i18n)
app.use(VueQueryPlugin)
app.use(PrimeVue, {
  theme: {
    preset: Aura,
    options: {
      darkModeSelector: '.app-theme-dark',
    },
  },
})

app.mount('#app')
