"use client";

import Image from "next/image";
import { useTranslation } from "react-i18next";
import VerifiedBadge from "@/components/ui/VerifiedBadge";
import { getDefaultProfileImage } from "@/lib/helpers";
import { type PublicProfileResponse } from "./commonProfileTypes";
import { useLanguageStore } from "@/store/languageStore";
import { asset, badgeImage, type ProfileData } from "./commonProfileTypes";

/** The avatar, name, badge and headline statistic at the top of a public profile. */
export default function ProfileHeader({
  profile,
  badge,
  userType,
}: {
  profile: ProfileData;
  badge?: PublicProfileResponse["badge_info"];
  userType: PublicProfileResponse["user_type"];
}) {
  const { t } = useTranslation();
  const language = useLanguageStore((state) => state.language);
  const defaultImage =
    userType === "organization"
      ? asset("profile/org_profile.svg")
      : getDefaultProfileImage(
          profile.gender_display?.value_en,
          asset("profile/male_profile.svg"),
          asset("profile/female_profile.svg"),
          asset("profile/org_profile.svg")
        );
  const badgeSrc = badgeImage(badge);
  const secondaryValue =
    userType === "organization" ? profile.registration_number : profile.manual_id;

  return (
    <section className="border-b border-black/20 px-3 pt-10 2xl:px-5 2xl:pt-[70px]">
      <div
        className={`relative flex items-center gap-[100px] px-[75px] pb-10 lg:pb-[70px] md:gap-[60px] md:px-0 mobilescreen:flex-col mobilescreen:gap-8 mobilescreen:px-0 ${
          language === "ar"
            ? "2xl:right-[8%] lg:right-[4%] md:right-[2%]"
            : "2xl:left-[8%] lg:left-[4%] md:left-[2%]"
        }`}
      >
        <div className="flex items-center gap-[50px] mobilescreen:w-full mobilescreen:flex-col mobilescreen:gap-8">
          <Image
            src={profile.profile_pic || defaultImage}
            alt={profile.display_name || profile.nickname || "Profile"}
            width={168}
            height={168}
            className="h-[168px] w-[168px] rounded-full border-[5px] border-primary-5 object-cover"
            unoptimized
          />
          <div className="flex mobilescreen:w-full mobilescreen:justify-center xss:gap-4">
            <div className="details mobilescreen:w-full">
              <h1 className="text-lg font-bold text-primary-5 mobilescreen:text-base">
                {t("COMMON.NICKNAME")}
              </h1>
              {/* `display_name` first — `full_name` is null here by design.
                  BE-127's mark rides beside it, and renders nothing unless the
                  API said so. */}
              <p className="flex items-center gap-1.5 font-normal text-primary-5">
                {profile.display_name || profile.nickname || "—"}
                <VerifiedBadge verified={profile.is_verified} />
              </p>
              {secondaryValue && (
                <>
                  <h2 className="pt-10 font-bold text-primary-5 mobilescreen:pt-5">
                    {t(
                      userType === "organization"
                        ? "COMMON.ENTER_LICENSE_NUMBER"
                        : "COMMON.ID"
                    )}
                  </h2>
                  <p className="break-words font-normal text-primary-5">
                    {secondaryValue}
                  </p>
                </>
              )}
            </div>
            {badgeSrc && (
              <div className="hidden items-center gap-5 mobilescreen:flex">
                <div className="h-[120px] w-[2px] bg-black/20" />
                <Image
                  src={badgeSrc}
                  alt={badge?.name || "badge"}
                  width={70}
                  height={70}
                />
              </div>
            )}
          </div>
        </div>
        {badgeSrc && (
          <div className="flex items-center gap-[70px] mobilescreen:hidden">
            <div className="h-[176px] w-px bg-black/20" />
            <Image
              src={badgeSrc}
              alt={badge?.name || "badge"}
              width={90}
              height={90}
              className="h-auto w-[90px]"
            />
          </div>
        )}
      </div>
    </section>
  );
}
