import { createFileRoute, redirect } from '@tanstack/react-router'
import { useAuthStore } from '@/stores/auth-store'
import { AuthenticatedLayout } from '@/components/layout/authenticated-layout'

export const Route = createFileRoute('/_authenticated')({
  beforeLoad: ({ location }) => {
    const { user, accessToken } = useAuthStore.getState().auth
    if (!user || !accessToken) {
      throw redirect({
        to: '/sign-in',
        search: { redirect: location.href },
      })
    }
    if (user.role !== 'admin' || user.status !== 'active') {
      throw redirect({ to: '/403' })
    }
  },
  component: AuthenticatedLayout,
})
