import { cookies } from 'next/headers';
import { createServerClient } from '@/shared/db/supabase-client';

// --- ASA GENERATED START ---
// Session helpers for server-side auth operations.
// --- ASA GENERATED END ---

// --- USER CODE START ---

export async function getCurrentUser() {
  const cookieStore = await cookies();
  const supabase = createServerClient(cookieStore);
  const { data: { user }, error } = await supabase.auth.getUser();
  if (error || !user) return null;
  return user;
}

export async function requireUser() {
  const user = await getCurrentUser();
  if (!user) {
    throw new Error('Authentication required');
  }
  return user;
}

export async function getUserProfile() {
  const user = await requireUser();
  const cookieStore = await cookies();
  const supabase = createServerClient(cookieStore);
  const { data: profile } = await supabase
    .from('profiles')
    .select('*')
    .eq('id', user.id)
    .single();
  return { user, profile };
}

// --- USER CODE END ---
