"use client";

import Image from "next/image";
import { useTranslation } from "react-i18next";
import { asset } from "./volunteerAccountConstants";
import { type CombinedFormValues } from "./volunteerAccountTypes";

/**
 * The profile photo and the name under it.
 *
 * The photo saves on its own the moment it is cropped, not with the rest of the
 * form — so this is the one field that shows its own spinner and its own error.
 */
export default function VolunteerAvatarField({
  values,
  profilePic,
  uploadError,
  handleFileChange,
  getDefaultImage,
}: {
  values: CombinedFormValues;
  profilePic: string | null;
  uploadError: string | null;
  handleFileChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
  getDefaultImage: () => string;
}) {
  const { t } = useTranslation();
  return (
    <>
                      <div className="relative w-[168px] h-[168px] mx-auto">
                        <Image
                          src={profilePic || getDefaultImage()}
                          alt={t("COMMON.PROFILE_PICTURE")}
                          fill
                          unoptimized
                          className="rounded-full border-[5px] border-[#29246D] object-cover"
                        />
                        <div className="absolute bottom-2 right-2">
                          <input
                            type="file"
                            id="fileUpload"
                            name="profile_pic_file"
                            className="hidden"
                            accept="image/png,image/jpeg,image/webp"
                            onChange={handleFileChange}
                          />
                          <label htmlFor="fileUpload" className="cursor-pointer">
                            <Image
                              src={asset("profile/uploadicon1.svg")}
                              alt={t("COMMON.UPLOAD_IMAGE")}
                              width={30}
                              height={30}
                              className="w-[30px] h-[30px]"
                            />
                          </label>
                          {uploadError && (
                            <p className="text-red-500 text-sm mt-2">
                              {uploadError}
                            </p>
                          )}
                        </div>
                      </div>

                      <div className="text-center pt-5 2xl:pb-[50px] lg:pb-[10px] pb-[20px]">
                        <div className="text-primary-5 font-bold text-[25px] mb-2">
                          {values.first_name} {values.last_name}
                        </div>
                      </div>
    </>
  );
}
