import type { ReactiveAuthStore } from '~/plugins/pocketbase'

/**
 * Returns the PocketBase client and a reactive ref of the auth store.
 */
export const usePocketBase = () => {
  const { $pb } = useNuxtApp()
  if (!$pb) throw new Error('Pocketbase plugin not accessible')
  return {
    pb: $pb,
    /**
     * Reactive wrapper around the PocketBase auth store.
     * Access state via `authStore.value.isValid`, `.record`, `.token`, `.isSuperuser` etc.
     * Use only for reactive access; for imperative methods use `$pb.authStore` directly.
     */
    authStore: ($pb.authStore as ReactiveAuthStore).ref
  }
}
