// Gedeelde server-side Supabase-client (service role). Gebruikt door de
// /api/sync/*-routes om browserdata kantoorbreed beschikbaar te maken.
// Zonder SUPABASE_URL/SUPABASE_SERVICE_ROLE_KEY blijft de opslag lokaal
// (IndexedDB) — alle aanroepers moeten dat geval verdragen.

import { createClient, type SupabaseClient } from "@supabase/supabase-js";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyClient = SupabaseClient<any, any, any>;

let client: AnyClient | null | undefined;

export function maakServiceClient(): AnyClient | null {
  if (client !== undefined) return client;
  const url = process.env.SUPABASE_URL;
  const key = process.env.SUPABASE_SERVICE_ROLE_KEY;
  client = url && key ? createClient(url, key, { auth: { persistSession: false } }) : null;
  return client;
}

export function heeftPersistenteOpslag(): boolean {
  return Boolean(process.env.SUPABASE_URL && process.env.SUPABASE_SERVICE_ROLE_KEY);
}
