"use client";
import { hasOpportunityEnded } from "@/features/shared/clientReviewPolicy";

import type { ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { DETAIL_TITLE } from "./opportunityDetailLayout";
import { type InterestTag } from "@/lib/interests";
import { type LearnServeOpportunityData } from "./learnServeDetailsTypes";
import LearnServeFacts from "./LearnServeFacts";
import LearnServeDescriptionBlock from "./LearnServeDescriptionBlock";
import LearnServeCompletionGallery from "./LearnServeCompletionGallery";
import OpportunityFeedback from "./OpportunityFeedback";
import AssetIcon from "@/components/ui/AssetIcon";
import RegistrationDeadlineText from "./RegistrationDeadlineText";
import RejectionNotice from "./volunteer-event/RejectionNotice";
import { learnServeRejection } from "./learnServeDetailsViewState";

interface LearnServeDetailsColumnProps {
  id: string;
  selectedLanguage: string;
  opportunityData: LearnServeOpportunityData | undefined;
  isCreator: boolean;
  isInPerson: boolean;
  remainingParticipants: number;
  totalDuration: { hrs: number; mins: number } | null;
  description: string;
  interestTags: InterestTag[];
  afterCompletedImages: Array<{ id: number; image?: string }>;
  pendingFiles: File[];
  setPendingFiles: (files: File[] | ((previous: File[]) => File[])) => void;
  /** Returns the upload, so «حفظ» stays disabled until it settles. */
  onUpload: (files: File[]) => void | Promise<unknown>;
  onDownload: (imageId: number, imageName?: string) => void;
  onDelete: (imageId: number) => void;
  /** Rendered inside `LearnServeFacts` at narrow widths. */
  actionButton: (mobile: boolean) => ReactNode;
}

/**
 * The right-hand column: the title, the two headline facts above the fold, the
 * fact grid, the description, the post-completion gallery and the feedback form.
 *
 * Due date and learning type stay written out here rather than joining
 * `LearnServeFacts`: they sit above the title's fold as their own rows, and the
 * fact grid below is a two-column layout they were never part of.
 */
export default function LearnServeDetailsColumn({
  id,
  selectedLanguage,
  opportunityData,
  isCreator,
  isInPerson,
  remainingParticipants,
  totalDuration,
  description,
  interestTags,
  afterCompletedImages,
  pendingFiles,
  setPendingFiles,
  onUpload,
  onDownload,
  onDelete,
  actionButton,
}: LearnServeDetailsColumnProps) {
  const { t } = useTranslation();
  const rejection = learnServeRejection(opportunityData, isCreator);

  return (
    <div className="min-w-0">
      <div className="w-full">
        <div className="flex items-start justify-between gap-5">
        <div className="min-w-0 flex-1">
        <div className="flex justify-between items-start">
          <h2 className={DETAIL_TITLE}>
            {(opportunityData?.primary_language === "ar"
              ? opportunityData?.title_ar
              : opportunityData?.title_en) || ""}
          </h2>
        </div>
        {rejection && <RejectionNotice reason={rejection.reason} />}

        <div className="flex items-center text-gray-600 text-sm pb-3">
          <p className="text-primary-5 text-sm md:text-base font-bold">
            {t("COMMON.DUE_DATE")} :
            <span className="text-secondary-102 font-bold">
              {" "}
              {/* No due date is a real answer (optional since 2026-10-14):
                  registration closes a fixed lead before the start. */}
              <RegistrationDeadlineText at={opportunityData?.registration_closes_at} legacyDate={opportunityData?.due_date} language={selectedLanguage} />
            </span>
          </p>
        </div>

        <div className="flex items-center text-gray-600 text-sm pb-3 gap-2">
          <AssetIcon src="/assets/homepage/learn_type.svg" alt="" className={`${selectedLanguage === "ar" ? "ml-3" : "mr-3"} w-5 h-5 object-contain`} size={20} />
          <p className="text-primary-5 text-sm md:text-base font-bold">
            {" "}
            {t("COMMON.TYPE")} :
            <span className="text-secondary-102 font-bold">
              {" "}
              {
                opportunityData?.learning_type_display?.[
                selectedLanguage === "ar" ? "value_ar" : "value_en"
                ]
              }
            </span>
          </p>
        </div>

        <LearnServeFacts
          opportunityData={opportunityData}
          selectedLanguage={selectedLanguage}
          isCreator={isCreator}
          isInPerson={isInPerson}
          remainingParticipants={remainingParticipants}
          totalDuration={totalDuration}
          actionButton={actionButton}
        />
        <LearnServeDescriptionBlock
          selectedLanguage={selectedLanguage}
          description={description}
          interestTags={interestTags}
        />
        </div>
        {actionButton(false)}
        </div>
        <LearnServeCompletionGallery
          id={id}
          isCreator={isCreator}
          selectedLanguage={selectedLanguage}
          opportunityStatus={opportunityData?.opportunity_status}
          opportunityRecordId={opportunityData?.id}
          afterCompletedImages={afterCompletedImages}
          pendingFiles={pendingFiles}
          setPendingFiles={setPendingFiles}
          onUpload={onUpload}
          onDownload={onDownload}
          onDelete={onDelete}
        />
      </div>

      {hasOpportunityEnded(opportunityData) &&
        opportunityData?.id && (
          <OpportunityFeedback
            opportunityId={opportunityData.id}
            isAttended={opportunityData.is_attended === true}
          />
        )}
    </div>
  );
}
