import { createRouter, createWebHistory } from 'vue-router'
import { useUsersStore } from '@/stores/usersStore'
import Dashboard from '@/views/Dashboard.vue'
import Auth from '@/module/auth/routes'
import Uyeler from '@/module/uyeler/routes'
import Cekilisler from '@/module/cekilisler/routes'
import Kullanicilar from '@/module/kullanicilar/routes'
import SiteYonetimi from '@/module/site-yonetimi/routes'
import Settings from '@/module/ayarlar/routes'
import Logs from '@/module/sistem-gunlugu/routes'
import Profil from '@/module/Profil/routes'
import Muhasebe from '@/module/muhasebe/routes'

const router = createRouter({
  sensitive: undefined,
  strict: undefined,
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    ...Auth,
    ...Uyeler,
    ...Cekilisler,
    ...Kullanicilar,
    ...SiteYonetimi,
    ...Settings,
    ...Logs,
    ...Profil,
    ...Muhasebe,
    {
      path: '/',
      name: 'Dashboard',
      component: Dashboard,
      meta: {
        authRequired: true
      }
    }
  ]
})
router.beforeEach((to, from) => {
  const usersStore = useUsersStore()

  if (to.meta.authRequired && !usersStore.userIsAuth && from.name !== 'Login') {
    router.push('/login')
  }
})

export default router
