import { getProfileImageUrl } from "@/app/lib/profileImage";

interface Props {
  user: any;
  size: number;
  className?: string;
  /** Overrides the resolved user image — used for an instant local preview while uploading. */
  overrideSrc?: string | null;
}

/*
|--------------------------------------------------------------------------
| Profile Avatar
|--------------------------------------------------------------------------
|
| Shows the user's real uploaded photo when one exists, otherwise a
| branded initials circle — never a stock placeholder photo of a
| stranger (the previous behaviour across Header/EditProfileModal).
|
*/

export default function ProfileAvatar({ user, size, className, overrideSrc }: Props) {
  const src = overrideSrc || getProfileImageUrl(user);

  const name = user?.Name || user?.name || "";

  const initial = name.trim().charAt(0).toUpperCase() || "U";

  if (src) {
    return (
      <img
        src={src}
        alt={name || "Profile"}
        className={className}
        style={{
          width: size,
          height: size,
          borderRadius: "50%",
          objectFit: "cover",
        }}
      />
    );
  }

  return (
    <div
      className={className}
      style={{
        width: size,
        height: size,
        borderRadius: "50%",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        background: "linear-gradient(135deg, #f8c100, #e98921)",
        color: "#1a1206",
        fontWeight: 700,
        fontSize: Math.round(size * 0.4),
        margin: "0px auto",
      }}
    >
      {initial}
    </div>
  );
}
