/**
 * Client-side API helper for authenticated requests
 * Since token is stored as HTTP-only cookie, we cannot access it from client-side
 * Instead, we make requests through Next.js API routes that have access to cookies
 */

/**
 * Make authenticated API request to external backend through Next.js proxy
 * @param endpoint - The backend API endpoint (e.g., "user/profile")
 * @param options - Fetch options (method, body, etc.)
 */
export async function authenticatedFetch(
  endpoint: string,
  options: RequestInit = {}
) {
  const response = await fetch(`/api/proxy/${endpoint}`, {
    ...options,
    credentials: 'include', // Important: include cookies
    headers: {
      'Content-Type': 'application/json',
      ...options.headers,
    },
  });

  return response;
}

/**
 * Check if user is authenticated
 */
export async function checkAuthentication(): Promise<{
  authenticated: boolean;
  user?: any;
}> {
  try {
    const response = await fetch('/api/auth/check', {
      method: 'GET',
      credentials: 'include',
    });
    return await response.json();
  } catch (error) {
    console.error('Auth check failed:', error);
    return { authenticated: false };
  }
}
