"use client";

import type { ChoiceDisplay } from "@/lib/choices";
import BackgroundSocialLinks from "./BackgroundSocialLinks";
import ProfileStatCard from "./ProfileStatCard";
import { hasPositiveCount } from "@/lib/helpers";
import { asset, convertDecimalHoursToDisplay } from "./volunteerProfileDisplay";
import { useTranslation } from "react-i18next";
import VerifiedBadge from "@/components/ui/VerifiedBadge";

import { Badge } from "@/components/ui/Badge";
import Title from "@/components/shared/Title";
import { useLanguageStore } from "@/store/languageStore";

interface BackgroundInformationProps {
  company_name?: string | null;
  /** BE-127 — governmental, computed by type. Renders nothing when absent. */
  is_verified?: boolean | null;
  /* `ChoiceDisplay`, and `| null` rather than just optional:
     `masterChoiceCollection()` returns **null**, not `[]`, for an organization
     with no interests, and the choice's own `value_en`/`value_ar` are nullable.
     The guard below (`interests && interests.length > 0`) already handled it. */
  interests?: ChoiceDisplay[] | null;
  instagram_link?: string | null;
  facebook_link?: string | null;
  whatsapp_link?: string | null;
  linkedin_link?: string | null;
  twitter_link?: string | null;
  isVolunteerTeam?: boolean;
  organization_hours?: number | string | null;
  learn_opportunity_organized?: number | string | null;
  vol_opportunity_organized?: number | string | null;
  sponsored_count?: number | string | null;
}

export default function OrganizerBackgroundInformation({
  company_name,
  is_verified,
  interests,
  instagram_link,
  facebook_link,
  whatsapp_link,
  linkedin_link,
  twitter_link,
  isVolunteerTeam = false,
  organization_hours,
  learn_opportunity_organized,
  vol_opportunity_organized,
  sponsored_count,
}: BackgroundInformationProps) {
  const { t } = useTranslation();
  const currentLanguage = useLanguageStore((s) => s.language);

  // Team profiles hide the "sponsored" card, so their cards are slightly taller.
  const cardHeight = isVolunteerTeam
    ? "xl:h-[210px] lg:h-[210px] md:h-[230px] msscreen1:h-[215px] miniscreen10:h-[230px] xss3:h-[230px]"
    : "xl:h-[210px] lg:h-[210px] md:h-[210px] msscreen1:h-[215px] miniscreen10:h-[210px] xss3:h-[230px]";
  const labelHeight = isVolunteerTeam ? "" : "h-[48px]";

  const hasStats =
    hasPositiveCount(organization_hours) ||
    hasPositiveCount(vol_opportunity_organized) ||
    hasPositiveCount(learn_opportunity_organized) ||
    (!isVolunteerTeam && hasPositiveCount(sponsored_count));

  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 md:block gap-8 mb-12">
        {/* Background Info */}
        <div className="2xl:w-1/2 laptopmain:w-1/2 lg:w-full mobilescreen:w-full">
          <h2 className="flex">
            <Title text={t("COMMON.ABOUT.ME")} variant="default" />
          </h2>
          <div className="lg:w-[80%]">
            <div className="pb-5 items-center">
              <h3 className="font-bold inline-block text-primary-5 text-lg laptopmain:text-xl xss:text-base">
                {t("COMMON.COMPANYNAME")} :
              </h3>
              <span className="text-primary-5 text-lg xss:text-base">
                {company_name}
                {/* BE-127 — beside the entity's own name, same mark the public
                    profile shows. Nothing renders unless the API said so. */}
                <VerifiedBadge verified={is_verified} className="ms-1.5 align-text-bottom" />
              </span>
            </div>

            {/*
              Sector was dropped from the UI at the client's request. The field
              still exists on the API and will be removed there separately, so
              `sector_display` is simply no longer rendered.
            */}

            {interests && interests.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]">
                  {interests.map((interest) => (
                    <Badge
                      key={interest.id}
                      variant="outline"
                      className="bg-[#eaeaf1] text-primary-5 rounded-full px-4 py-2 font-normal xss:text-base"
                    >
                      {currentLanguage === "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}
              wrapperClassName="pb-5 items-center"
              linkClassName="text-primary-5"
            />
          </div>
        </div>

        {/* Statistics — a counter still at zero is hidden (the client's
            rule), and with none above zero the whole block goes. */}
        {hasStats && (
        <div
          className={`laptopmain:w-[80%] w-full ${
            isVolunteerTeam ? "2xl:w-[70%]" : "2xl:w-[45%]"
          }`}
        >
          <h2 className="flex">
            <Title text={t("COMMON.ACHIEVEMENT")} variant="default" />
          </h2>
          <div
            className={
              isVolunteerTeam
                ? "grid 2xl:grid-cols-3 lg:grid-cols-2 md:grid-cols-3 mobilescreen:grid-cols-1 gap-[30px] mobilescreen:gap-5"
                : "grid grid-cols-2 xss:grid-cols-1 gap-[30px] xss:gap-5"
            }
          >
            {hasPositiveCount(organization_hours) && (
              <ProfileStatCard
                icon={asset("profile/statistics/n_Volunteerhours.svg")}
                alt=""
                value={convertDecimalHoursToDisplay(organization_hours)}
                label={t("COMMON.VOLUNTEER_HOURS-")}
                color="hours"
                heightClass={cardHeight}
                labelHeightClass={labelHeight}
              />
            )}
            {hasPositiveCount(vol_opportunity_organized) && (
              <ProfileStatCard
                icon={asset("profile/statistics/n_volunteer_organization.svg")}
                alt=""
                value={vol_opportunity_organized || 0}
                label={t("COMMON.VOLUNTEER_OPPORTUNITIES-")}
                color="volunteer"
                heightClass={cardHeight}
                labelHeightClass={labelHeight}
              />
            )}
            {hasPositiveCount(learn_opportunity_organized) && (
              <ProfileStatCard
                icon={asset("profile/statistics/n_learnServeicn.svg")}
                alt=""
                value={learn_opportunity_organized || 0}
                label={t("COMMON.OPPORTUNITIESORGANIZED--")}
                color="learnServe"
                heightClass={cardHeight}
                labelHeightClass={labelHeight}
              />
            )}
            {!isVolunteerTeam && hasPositiveCount(sponsored_count) && (
              <ProfileStatCard
                icon={asset("profile/statistics/n_sponseredbyus.svg")}
                alt=""
                value={sponsored_count || 0}
                label={t("COMMON.SPONSERED.ORGANIZED")}
                color="sponsored"
                heightClass={cardHeight}
                labelHeightClass={labelHeight}
              />
            )}
          </div>
        </div>
        )}
      </div>
    </div>
  );
}
