
// middleware/auth.ts

import { useAccess } from '~/composables/useAccess'
export default defineNuxtRouteMiddleware((to) => {
  const { getRole, getPermissionsByPath } = useAccess()
  const role = useAccess()
  // const perms = getPermissionsByPath(to.path)

  // const meta = to.meta

  //  Jika belum login, redirect ke halaman login
  const isLoggedIn = !!role // role 
  if (!isLoggedIn) {
    console.log('[ NOT LOGGED IN]', { path: to.path })
    //return navigateTo('/login') //  
  }

  // //  Validasi permission 
  // const rolePass = !meta.role || meta.role.includes(role)
  // const permissionPass = !meta.permission || meta.permission.every(p => perms.includes(p))

  // if (!rolePass || !permissionPass) {
  //   console.log('[ ACCESS DENIED]', {
  //     path: to.path,
  //     role,
  //     requiredRole: meta.role,
  //     permissions: perms,
  //     requiredPermission: meta.permission
  //   })
  //   return navigateTo('/403') //  Halaman tidak punya akses
  // }
    return navigateTo('/sample-page-copy')
  // Semua valid, lanjut ke halaman
  // console.log('[ACCESS GRANTED]', {
  //   path: to.path,
  //   role,
  //   permissions
  // })
})
