import { useAuthStore } from '@/store/auth-store'
import { useAuthModalStore } from '@/store/auth-modal-store'

export function useRequireAuth() {
  const user = useAuthStore((s) => s.user)
  const isLoading = useAuthStore((s) => s.isLoading)
  const openModal = useAuthModalStore((s) => s.open)

  /** Returns true if authenticated. If guest, opens auth modal and returns false. */
  function requireAuth(): boolean {
    if (user) return true
    openModal('register')
    return false
  }

  return { isAuthenticated: !!user, isLoading, requireAuth }
}
