/**
 * Convert avatar URL to proper backend URL
 * Handles both S3 URLs and relative backend paths
 */
export function getProxyImageUrl(url: string | null | undefined): string {
  if (!url) {
    return '';
  }

  // If it's a data URL, return as-is
  if (url.startsWith('data:')) {
    return url;
  }

  const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api/v1';
  const apiBaseUrl = apiUrl.replace('/api/v1', '');

  // If it's a relative path from backend (like /development/avatars/...)
  // Prefix with backend base URL
  if (url.startsWith('/development/') || url.startsWith('/uploads/') || url.startsWith('/avatars/')) {
    return `${apiBaseUrl}${url}`;
  }

  // If it's an S3/external URL, proxy through backend
  if (url.startsWith('http')) {
    return `${apiUrl}/upload/image?url=${encodeURIComponent(url)}`;
  }

  // For other relative paths, assume they're backend paths
  if (url.startsWith('/')) {
    return `${apiBaseUrl}${url}`;
  }

  return url;
}
