import { createContext, useContext } from 'react'
import type { Role, SessionUser, TokenBundle } from '@/types'

export interface AuthContextValue {
  user: SessionUser | null
  roles: Role[]
  isAdmin: boolean
  isAuthenticated: boolean
  isLoading: boolean
  onLoginSuccess: (tokenBundle: TokenBundle) => Promise<void>
  logout: () => Promise<void>
  refreshUser: () => Promise<void>
}

export const AuthContext = createContext<AuthContextValue | null>(null)

export function useAuth(): AuthContextValue {
  const context = useContext(AuthContext)
  if (!context) {
    throw new Error('useAuth must be used within an AuthProvider')
  }

  return context
}
