"use client";

import { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import LearnServeAudienceTerms from "./LearnServeAudienceTerms";
import { LearnServeOpportunityData } from "./learnServeDetailsTypes";
import AssetIcon from "@/components/ui/AssetIcon";
import { formatHoursMinutes } from "@/features/shared/clientReviewPolicy";
import { formatPriceTag } from "@/lib/helpers";

interface LearnServeTermsProps {
  opportunityData?: LearnServeOpportunityData | null;
  selectedLanguage: string;
  /** In person rather than online — decides whether an address is shown. */
  isInPerson: boolean;
  isCreator: boolean;
  /** Places still open, already clamped at zero by the caller. */
  remainingParticipants: number;
  /** Total commitment across the whole opportunity. */
  totalDuration: { hrs: number; mins: number } | null;
  /** The register / manage control, rendered inline on a phone. */
  actionButton: (mobile: boolean) => ReactNode;
}

/**
 * The opportunity's terms: where it happens, its certificate, and who may
 * attend — plus the phone copy of the register button, which sits at the end
 * of this block rather than in the header.
 */
export default function LearnServeTerms({
  opportunityData,
  selectedLanguage,
  isInPerson,
  isCreator,
  remainingParticipants,
  totalDuration,
  actionButton,
}: LearnServeTermsProps) {
  const { t } = useTranslation();

  return (
    <div className="border-b mb-6">
      <div className="flex mt-5 mobilescreen:mt-3.5 xss:flex-col">
        <div className="w-1/2 xss:w-full">
          {totalDuration && (
            <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
              <AssetIcon src="/assets/homepage/hours.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
              <p className="text-base font-bold text-primary-5" dir="ltr">{formatHoursMinutes(totalDuration.hrs + totalDuration.mins / 60)}</p>
            </div>
          )}

          <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
            <AssetIcon src="/assets/voluneteerevent/age.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
            <p className="text-secondary-102 text-sm md:text-base font-bold">
              {t("COMMON.AGE")} :
            </p>
            <p className="text-sm md:text-base font-bold text-primary-5">
              {opportunityData?.from_age}
              {opportunityData?.to_age ? (
                <>
                  <span className="text-secondary-102 font-bold"> - </span>
                  {opportunityData?.to_age}
                </>
              ) : (
                <span className="text-primary-5 font-bold"> + </span>
              )}
            </p>
          </div>

          <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
            <AssetIcon src="/assets/homepage/person.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
            <p className="text-sm md:text-base font-bold text-primary-5">
              {remainingParticipants}
            </p>
            <p className="text-secondary-102 text-sm md:text-base font-bold">
              {t("COMMON.NEEDED")}
            </p>
          </div>

          <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
            <AssetIcon src="/assets/voluneteerevent/gender.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
            <p className="text-sm md:text-base font-bold text-primary-5">
              {t("COMMON.GENDER")} :
            </p>
            <p className="text-secondary-102 text-sm md:text-base font-bold">
              {
                opportunityData?.gender_display?.[
                selectedLanguage === "ar" ? "value_ar" : "value_en"
                ]
              }
            </p>
          </div>
          {/* A paid opportunity's price, in the same «33 د.ك» form as the
              card's pill. */}
          {opportunityData?.is_paid && formatPriceTag(opportunityData.price, t) ? (
            <div className="flex items-center gap-2 mb-5 mobilescreen:mb-3.5">
              <AssetIcon src="/assets/voluneteerevent/price.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
              <p className="text-sm md:text-base font-bold text-primary-5">
                {t("COMMON.PRICE")} :
              </p>
              <p className="text-secondary-102 text-sm md:text-base font-bold">
                {formatPriceTag(opportunityData.price, t)}
              </p>
            </div>
          ) : null}
        </div>

        <LearnServeAudienceTerms
          opportunityData={opportunityData}
          selectedLanguage={selectedLanguage}
          isInPerson={isInPerson}
          isCreator={isCreator}
        />

        <div className="hidden xss:block">{actionButton(true)}</div>
      </div>
    </div>
  );
}
