'use client'

import { createContext, useContext } from 'react'

export type AdminRole = 'SUPER_ADMIN' | 'ADMIN' | 'SUPPORT' | 'FINANCE' | 'VIEWER'

export type AdminSessionUser = {
  id: string
  email: string
  role: AdminRole
  totpEnabled?: boolean
}

const METRICS_ALLOWED_ROLES = new Set<AdminRole>(['SUPER_ADMIN', 'ADMIN', 'SUPPORT', 'FINANCE'])

const AdminSessionContext = createContext<AdminSessionUser | null>(null)

export function AdminSessionProvider({
  admin,
  children,
}: {
  admin: AdminSessionUser
  children: React.ReactNode
}) {
  return <AdminSessionContext.Provider value={admin}>{children}</AdminSessionContext.Provider>
}

export function useAdminSession() {
  return useContext(AdminSessionContext)
}

export function canAccessAdminMetrics(admin: Pick<AdminSessionUser, 'role' | 'totpEnabled'> | null | undefined) {
  return Boolean(admin && admin.totpEnabled && METRICS_ALLOWED_ROLES.has(admin.role))
}
