/*
|--------------------------------------------------------------------------
| Profile Image Resolution
|--------------------------------------------------------------------------
|
| The Go backend's exact field name for the uploaded profile photo
| isn't documented (the Postman collection has no sample response),
| and the app already handles both PascalCase (Go JSON) and
| camelCase/lowercase user objects elsewhere (see Header.tsx's
| `user.Name || user.name`) — so check every plausible alias.
|
*/

const IMAGE_FIELD_ALIASES = [
  "Profile",
  "ProfileImage",
  "ProfilePicture",
  "ProfilePhoto",
  "Avatar",
  "Photo",
  "Image",
  "profileImage",
  "profile_image",
  "profilePicture",
  "profile_picture",
  "avatar",
  "photo",
  "image",
];

export function getProfileImageUrl(user: any): string | null {
  if (!user || typeof user !== "object") {
    return null;
  }

  for (const key of IMAGE_FIELD_ALIASES) {
    const value = user[key];

    if (typeof value === "string" && value.trim()) {
      return resolveImageUrl(value.trim());
    }
  }

  return null;
}

function resolveImageUrl(path: string): string {
  if (/^https?:\/\//i.test(path) || path.startsWith("data:")) {
    return path;
  }

  const normalized = path.startsWith("/") ? path : `/${path}`;

  return `https://api.astrology.togwe.com${normalized}`;
}
