import { createClient, SupabaseClient } from '@supabase/supabase-js';
import { logger } from './debugConfig';

const supabaseUrl = import.meta.env.VITE_SUPABASE_URL;
const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY;

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

logger.info('supabase-client', '🔄 Initializing Supabase client', {
  url: supabaseUrl,
  hasKey: !!supabaseAnonKey,
});

export const supabase: SupabaseClient = createClient(
  supabaseUrl,
  supabaseAnonKey,
  {
    auth: {
      flowType: 'pkce',
      autoRefreshToken: true,
      persistSession: true,
      detectSessionInUrl: false,
      storage: window.localStorage,
      storageKey: 'supabase.auth.token',
      debug: true
    },
    global: {
      headers: {
        'X-Client-Info': 'classroom-copilot',
      },
    },
    db: {
      schema: 'public'
    }
  }
);