import { createClient } from '@supabase/supabase-js';
import * as dotenv from 'dotenv';

// Both clients run on the server only. `supabase` uses the anon key; `supabaseAdmin` uses the service role
// for Auth admin APIs (create/delete users, verify tokens, etc.). Never expose the service key to the client.

dotenv.config();

const supabaseUrl = process.env.SUPABASE_URL!;
const supabaseAnonKey = process.env.SUPABASE_ANON_KEY!;
const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY!;

if (!supabaseUrl || !supabaseAnonKey || !supabaseServiceKey) {
  throw new Error('Missing Supabase environment variables');
}

export const supabase = createClient(supabaseUrl, supabaseAnonKey);

export const supabaseAdmin = createClient(supabaseUrl, supabaseServiceKey, {
  auth: {
    autoRefreshToken: false,
    persistSession: false
  }
});

export default supabase;
