'use client'

import { createContext, useContext } from 'react'
import { useAuth } from '@/hooks'
/* import { AccountType } from '@/types'
 */
type PermissionContextType = {
  canAccess: (requiredRoles: any[]) => boolean
  currentRole: any | null
}

const PermissionContext = createContext<PermissionContextType>({
  canAccess: () => false,
  currentRole: null
})

export const PermissionProvider = ({ children }: { children: React.ReactNode }) => {
  const { user } = useAuth()

  const canAccess = (requiredRoles: any[]) => {
    if (!user) return false
    return requiredRoles.includes(user.account_type)
  }

  return (
    <PermissionContext.Provider
      value={{
        canAccess,
        currentRole: user?.account_type || null
      }}
    >
      {children}
    </PermissionContext.Provider>
  )
}

export const usePermission = () => useContext(PermissionContext)
