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

// Runtime env (injected by docker/entrypoint.sh) takes priority over build-time vars
const runtimeEnv = (window as unknown as { __ENV__?: Record<string, string> }).__ENV__ ?? {}

const supabaseUrl =
  runtimeEnv.VITE_SUPABASE_URL ||
  import.meta.env.VITE_SUPABASE_URL

const supabaseAnonKey =
  runtimeEnv.VITE_SUPABASE_ANON_KEY ||
  runtimeEnv.VITE_SUPABASE_PUBLISHABLE_KEY ||
  import.meta.env.VITE_SUPABASE_ANON_KEY ||
  import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY

if (!supabaseUrl || !supabaseAnonKey) {
  console.warn(
    "Supabase env vars missing. Set VITE_SUPABASE_URL and VITE_SUPABASE_ANON_KEY (or VITE_SUPABASE_PUBLISHABLE_KEY) in .env",
  )
}

export const supabase = createClient(
  supabaseUrl || "https://placeholder.supabase.co",
  supabaseAnonKey || "placeholder-key",
)
