import { createClient } from '@supabase/supabase-js'

const supabaseUrl = import.meta.env.VITE_SUPABASE_URL as string
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY as string
const authUrl = import.meta.env.VITE_AUTH_URL as string | undefined

/**
 * Supabase client singleton.
 * Returns null if env vars are not configured (local-only mode).
 */
export const supabase =
  supabaseUrl && supabaseAnonKey
    ? createClient(supabaseUrl, supabaseAnonKey, {
        auth: {
          persistSession: true,
          autoRefreshToken: true,
          detectSessionInUrl: true,
          ...(authUrl ? { url: authUrl } : {}),
        },
      })
    : null

export function isSupabaseConfigured(): boolean {
  return supabase !== null
}
