import { createContext, useContext, type ReactNode } from 'react'
import { useSessionStore } from '../stores/sessionStore'
import type { MockUser } from '../stores/sessionStore'

// Placeholder AuthContext — swap for real auth (Supabase, Auth0, etc.) later.
// All call sites use this context; no component imports sessionStore directly.

interface AuthContextValue {
  user: MockUser | null
  isAuthenticated: boolean
  isLoading: boolean
  login: (user: MockUser) => void
  logout: () => void
}

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

export function AuthProvider({ children }: { children: ReactNode }) {
  const { currentUser, isAuthenticated, login, logout } = useSessionStore()

  const value: AuthContextValue = {
    user: currentUser,
    isAuthenticated,
    isLoading: false, // always resolved in mock mode
    login,
    logout,
  }

  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
}

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