"use client";

import Image from "next/image";
import BackgroundSocialLinks from "./BackgroundSocialLinks";
import VolunteerBackgroundStats from "./VolunteerBackgroundStats";
import { asset } from "./volunteerProfileDisplay";
import { useTranslation } from "react-i18next";

import { Badge } from "@/components/ui/Badge";
import Title from "@/components/shared/Title";
import { healthConcernOptions, occupationOptions } from "@/data/Constants";
import { useLanguageStore } from "@/store/languageStore";

interface LocalizedValue {
  id: number;
  value_en: string;
  value_ar: string;
}

interface VolunteerBackgroundInformationProps {
  full_name?: string;
  occupation?: string;
  interest_display?: LocalizedValue[];
  health_concerns?: string;
  instagram_link?: string | null;
  facebook_link?: string | null;
  whatsapp_link?: string | null;
  linkedin_link?: string | null;
  twitter_link?: string | null;
  /**
   * Flat counters, used when the response carries no `statistics` object.
   */
  total_volunteer_hours?: number | string;
  total_opportunities?: number | string;
  total_certificates?: number | string;
  statistics?: {
    all_time?: {
      total_hours?: number | string;
      total_opportunities?: number | string;
      total_certificates?: number | string;
    };
  };
  /** The public profile hides health concerns. */
  isPublicProfile?: boolean;
}

/**
 * "About me" plus the achievement counters on the volunteer's own profile. The
 * organizer counterpart is OrganizerBackgroundInformation.
 */
export default function VolunteerBackgroundInformation({
  full_name,
  occupation,
  interest_display,
  health_concerns,
  instagram_link,
  facebook_link,
  whatsapp_link,
  linkedin_link,
  twitter_link,
  total_volunteer_hours,
  total_opportunities,
  statistics,
  isPublicProfile = false,
}: VolunteerBackgroundInformationProps) {
  const { t } = useTranslation();
  const selectedLanguage = useLanguageStore((s) => s.language);
  const localeKey = selectedLanguage === "ar" ? "name_ar" : "name_en";

  // The endpoint returns either a nested `statistics` block or flat counters.
  const stats = {
    volunteerHours:
      statistics?.all_time?.total_hours ?? total_volunteer_hours ?? 0,
    volunteerOpportunities:
      statistics?.all_time?.total_opportunities ?? total_opportunities ?? 0,
  };

  const localizedOccupation = occupationOptions.find(
    (option) => option.value === occupation,
  )?.[localeKey];

  const localizedHealthConcern = healthConcernOptions.find(
    (option) => option.value === health_concerns,
  )?.[localeKey];

  return (
    <div className="2xl:px-5 px-3 mobilescreen:px-[13px] 2xl:pt-[70px] laptopmain:2xl:pt-[40px] lg:pt-[40px] md:pt-[40px] pt-[40px]">
      <div className="lg:flex mb-12 md:block mobilescreen:block">
        {/* Background Info */}
        <div className="2xl:w-1/2 laptopmain:w-1/2 lg:w-full mobilescreen:w-full">
          <h2 className="text-start flex">
            <Title text={t("COMMON.ABOUT.ME")} variant="default" />
          </h2>
          <div className="lg:w-[80%] mobilescreen:lg:w-full">
            <div className="flex flex-wrap gap-2 pb-5 items-center">
              <h3 className="text-primary-5 font-bold text-lg mobilescreen:text-[16px]">
                {t("COMMON.FULL_NAME")} :
              </h3>
              <span className="text-primary-5 text-lg">{full_name}</span>
            </div>

            {occupation && (
              <div className="flex flex-wrap gap-2 pb-5 items-center">
                <h3 className="text-primary-5 font-bold text-lg mobilescreen:text-[16px]">
                  {t("COMMON.ENTER.OCCIPATION")} :
                </h3>
                <span className="text-primary-5 text-lg">
                  {localizedOccupation}
                </span>
              </div>
            )}

            {!isPublicProfile && health_concerns && (
              <div className="flex flex-wrap gap-2 pb-5 items-center">
                <h3 className="text-primary-5 font-bold text-lg mobilescreen:text-[16px]">
                  {t("COMMON.HEALTH_CONCERN")} :
                </h3>
                <span className="text-primary-5 text-lg">
                  {localizedHealthConcern}
                </span>
                <div className="relative inline-block group">
                  <Image
                    src={asset("voluneteerevent/linkinfo.svg")}
                    alt={t("COMMON.HEALTH_CONCERN")}
                    width={20}
                    height={20}
                    className="cursor-pointer w-5 h-5"
                  />
                  <div
                    className={`absolute ${
                      selectedLanguage === "ar"
                        ? "right-full mr-2"
                        : "left-full ml-2"
                    } top-1/2 transform -translate-y-1/2 bg-white rounded-lg shadow-lg opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all duration-200 w-[250px] z-50 p-3 xss:w-[220px] xs:w-[200px] xs:left-auto xs:right-0 xs:top-full xs:translate-y-0 xs:mt-2 ltr:xs:mr-0 ltr:xs:right-[-12px] rtl:xs:ml-0 rtl:xs:right-auto rtl:xs:left-[-24px]`}
                  >
                    {t("COMMON.HEALTH_CONCERN_INFO")}
                  </div>
                </div>
              </div>
            )}

            {interest_display && interest_display.length > 0 && (
              <div className="flex flex-wrap gap-2 pb-5 items-start flex-col">
                <div>
                  <h3 className="text-primary-5 font-bold text-lg mobilescreen:text-[16px]">
                    {t("COMMON.INTEREST")}
                  </h3>
                </div>
                <div className="flex flex-wrap gap-2 items-center rounded-[20px] border border-[#29246D] py-[12px] px-[17px]">
                  {interest_display.map((interest) => (
                    <Badge
                      key={interest.id}
                      variant="outline"
                      className="bg-[#eaeaf1] text-primary-5 rounded-full px-4 py-1 font-normal text-[15px]"
                    >
                      {selectedLanguage === "en"
                        ? interest.value_en
                        : interest.value_ar}
                    </Badge>
                  ))}
                </div>
              </div>
            )}

            <BackgroundSocialLinks
              facebook_link={facebook_link}
              twitter_link={twitter_link}
              whatsapp_link={whatsapp_link}
              instagram_link={instagram_link}
              linkedin_link={linkedin_link}
            />
          </div>
        </div>

        <VolunteerBackgroundStats stats={stats} />
      </div>
    </div>
  );
}
