import { ROLE_HOME_ROUTES } from '@/enums'
import { useAuthStore } from '@/store/auth'
import { useRoute, useRouter } from 'vue-router'

export const useRedirectAfterLogin = () => {
  const router = useRouter()
  const route = useRoute()
  const auth = useAuthStore()

  return () => {
    const redirect = route.query.redirect
    if (redirect && typeof redirect === 'string') {
      router.push(redirect).catch(() => router.push('/'))
      return
    }
    const target = { name: ROLE_HOME_ROUTES[auth.role] || 'student-dashboard' }
    router.push(target).catch(() => router.push('/'))
  }
}

export default useRedirectAfterLogin
