"use client";

import { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { formatSingleDate } from "@/lib/helpers";
import AddToCalendar from "@/components/shared/AddToCalendar";
import LearnServeTerms from "./LearnServeTerms";
import { clockTime } from "./learnServeDuration";
import { LearnServeOpportunityData } from "./learnServeDetailsTypes";
import AssetIcon from "@/components/ui/AssetIcon";

interface LearnServeFactsProps {
  opportunityData?: LearnServeOpportunityData | null;
  selectedLanguage: string;
  isCreator: boolean;
  /** In person rather than online — decides whether an address is shown. */
  isInPerson: 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 facts: dates, times, format, capacity, price, certificate
 * and audience.
 *
 * Every row is one field of the record with an icon beside it, which is why
 * this is a long list rather than a nested structure — and why it belongs in
 * its own file rather than in the middle of the page.
 */
export default function LearnServeFacts({
  opportunityData,
  selectedLanguage,
  isCreator,
  isInPerson,
  remainingParticipants,
  totalDuration,
  actionButton,
}: LearnServeFactsProps) {
  const { t } = useTranslation();
  const startClock = clockTime(opportunityData?.start_time);
  const endClock = clockTime(opportunityData?.end_time);

  return (
    <>
    <div className="grid grid-cols-1 md:grid-cols-2">
      <div className="flex items-center pb-3 gap-2">
        <AssetIcon src="/assets/homepage/dateicn.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.START.DATE")} :
        </p>
        <p className="text-secondary-102 text-sm md:text-base font-bold">
          {formatSingleDate(
            opportunityData?.start_date || "",
            selectedLanguage,
            t
          )}
        </p>
      </div>
      <div className="flex items-center pb-3 gap-2">
        <AssetIcon src="/assets/homepage/dateicn.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.END.DATE")} :
        </p>
        <p className="text-secondary-102 text-sm md:text-base font-bold">
          {formatSingleDate(
            opportunityData?.end_date || "",
            selectedLanguage,
            t
          )}
        </p>
      </div>
      {/* No daily time on the record (`start_time: null`): no row, rather
          than «Invalid date». */}
      {startClock && endClock && (
        <div className="flex items-center gap-2 pb-2 font-bold mobilescreen:pb-1.5 text-sm md:text-base">
          <AssetIcon src="/assets/homepage/timeicn.svg" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
          <p className="font-bold text-secondary-102">{startClock.format("hh:mm")}</p>
          <p className="text-primary-5 font-bold">
            {startClock.hour() < 12 ? t("COMMON.AM") : t("COMMON.PM")}{" "}
          </p>
          <p className="text-secondary-102 font-bold">- {endClock.format("hh:mm")}</p>
          <p className="text-primary-5 font-bold">
            {endClock.hour() < 12 ? t("COMMON.AM") : t("COMMON.PM")}
          </p>
        </div>
      )}
    </div>

    {/* Sits with the dates it copies — Google Calendar, or an .ics
        for Apple Calendar / Outlook. */}
    <div className="flex">
      <AddToCalendar
        payload={{
          title_en: opportunityData?.title_en,
          title_ar: opportunityData?.title_ar,
          location_en: opportunityData?.location_en,
          location_ar: opportunityData?.location_ar,
          start_date: opportunityData?.start_date,
          end_date: opportunityData?.end_date,
          start_time: opportunityData?.start_time,
          end_time: opportunityData?.end_time,
        }}
      />
    </div>

      <LearnServeTerms
        opportunityData={opportunityData}
        selectedLanguage={selectedLanguage}
        isInPerson={isInPerson}
        isCreator={isCreator}
        remainingParticipants={remainingParticipants}
        totalDuration={totalDuration}
        actionButton={actionButton}
      />
    </>
  );
}
