import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../stores/authStore'

// Views
import Entries from '../views/Entries.vue'
import Stats from '../views/Stats.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import PasswordForgot from '../views/PasswordForgot.vue'
import PasswordReset from '../views/PasswordReset.vue'
import OAuthCallback from '../views/OAuthCallback.vue'
import WeekOverview from '../views/WeekOverview.vue'
import Timefix from '../views/Timefix.vue'
import Vacation from '../views/Vacation.vue'
import Sick from '../views/Sick.vue'
import Workdays from '../views/Workdays.vue'
import Calendar from '../views/Calendar.vue'
import Holidays from '../views/Holidays.vue'
import Profile from '../views/Profile.vue'
import PasswordChange from '../views/PasswordChange.vue'
import Timewish from '../views/Timewish.vue'
import Roles from '../views/Roles.vue'
import Invite from '../views/Invite.vue'
import Permissions from '../views/Permissions.vue'
import Export from '../views/Export.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // Auth-Routes (öffentlich)
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { requiresGuest: true }
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      meta: { requiresGuest: true }
    },
    {
      path: '/password-forgot',
      name: 'password-forgot',
      component: PasswordForgot,
      meta: { requiresGuest: true }
    },
    {
      path: '/password-reset',
      name: 'password-reset',
      component: PasswordReset,
      meta: { requiresGuest: true }
    },
    {
      path: '/oauth-callback',
      name: 'oauth-callback',
      component: OAuthCallback
    },
    
    // Geschützte Routes
    {
      path: '/',
      redirect: '/bookings/week'
    },
    {
      path: '/bookings/week',
      name: 'week-overview',
      component: WeekOverview,
      meta: { requiresAuth: true }
    },
    {
      path: '/bookings/timefix',
      name: 'timefix',
      component: Timefix,
      meta: { requiresAuth: true }
    },
    {
      path: '/bookings/vacation',
      name: 'vacation',
      component: Vacation,
      meta: { requiresAuth: true }
    },
    {
      path: '/bookings/sick',
      name: 'sick',
      component: Sick,
      meta: { requiresAuth: true }
    },
    {
      path: '/bookings/workdays',
      name: 'workdays',
      component: Workdays,
      meta: { requiresAuth: true }
    },
    {
      path: '/calendar',
      name: 'calendar',
      component: Calendar,
      meta: { requiresAuth: true }
    },
    {
      path: '/admin/holidays',
      name: 'admin-holidays',
      component: Holidays,
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/admin/roles',
      name: 'admin-roles',
      component: Roles,
      meta: { requiresAuth: true, requiresAdmin: true }
    },
    {
      path: '/settings/profile',
      name: 'settings-profile',
      component: Profile,
      meta: { requiresAuth: true }
    },
    {
      path: '/settings/password',
      name: 'settings-password',
      component: PasswordChange,
      meta: { requiresAuth: true }
    },
    {
      path: '/settings/timewish',
      name: 'settings-timewish',
      component: Timewish,
      meta: { requiresAuth: true }
    },
    {
      path: '/settings/invite',
      name: 'settings-invite',
      component: Invite,
      meta: { requiresAuth: true }
    },
    {
      path: '/settings/permissions',
      name: 'settings-permissions',
      component: Permissions,
      meta: { requiresAuth: true }
    },
    {
      path: '/export',
      name: 'export',
      component: Export,
      meta: { requiresAuth: true }
    },
    {
      path: '/entries',
      name: 'entries',
      component: Entries,
      meta: { requiresAuth: true }
    },
    {
      path: '/stats',
      name: 'stats',
      component: Stats,
      meta: { requiresAuth: true }
    }
  ]
})

// Navigation Guards
router.beforeEach(async (to, from, next) => {
  const authStore = useAuthStore()
  
  // Session-Wiederherstellung beim ersten Laden
  if (!authStore.isAuthenticated && authStore.loadToken()) {
    try {
      await authStore.fetchCurrentUser()
    } catch (error) {
      console.error('Session-Wiederherstellung fehlgeschlagen:', error)
      authStore.clearAuth()
    }
  }

  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  const requiresGuest = to.matched.some(record => record.meta.requiresGuest)

  if (requiresAuth && !authStore.isAuthenticated) {
    // Geschützte Route aber nicht eingeloggt -> Login
    next({ name: 'login', query: { redirect: to.fullPath } })
  } else if (requiresGuest && authStore.isAuthenticated) {
    // Guest-Route aber bereits eingeloggt -> Wochenübersicht
    next({ name: 'week-overview' })
  } else {
    // Alles OK
    next()
  }
})

export default router

