import type {
  TTenderDetails,
  TTenderDocumentsResponse,
} from "@/lib/api/types/Tenders";
import { scrollToTenderSection } from "../scroll-to-section";
import { shouldShowLotsSection } from "../tender-display-utils";
import { shouldShowLocationSection } from "../tender-nav-utils";
import type { TenderNavItem } from "../types";
import { TenderAiSummarySection } from "./tender-ai-summary-section";
import { TenderDescriptionSection } from "./tender-description-section";
import { TenderDocumentsSection } from "./tender-documents-section";
import { TenderLocationSection } from "./tender-location-section";
import { TenderLotsSection } from "./tender-lots-section";
import { TenderProcurementSection } from "./tender-procurement-section";
import { TenderSectionNavList } from "./tender-section-nav-list";
import { TenderTimelineSection } from "./tender-timeline-section";
import { TenderTranslationSection } from "./tender-translation-section";

type TenderDetailsContentProps = {
  tender: TTenderDetails;
  tenderNavItems: TenderNavItem[];
  activeSectionId: string;
  tenderId: string;
  documents: TTenderDocumentsResponse[];
  documentsLoading: boolean;
  /** False once documents have loaded and the list is empty — section and nav are omitted. */
  showDocumentsSection: boolean;
};

export function TenderDetailsContent({
  tender,
  tenderNavItems,
  activeSectionId,
  tenderId,
  documents,
  documentsLoading,
  showDocumentsSection,
}: TenderDetailsContentProps) {
  const showLocation = shouldShowLocationSection(tender);

  return (
    <div className="px-4 pb-8 pt-2 sm:px-6 sm:pb-10 xl:px-8 xl:pb-10 xl:pt-3 2xl:px-10">
      <div className="xl:grid xl:grid-cols-[minmax(0,1fr)_13.5rem] xl:items-start xl:gap-10 2xl:grid-cols-[minmax(0,1fr)_15rem] 2xl:gap-12">
        <div className="min-w-0 space-y-6 xl:space-y-10">
          <div className="xl:hidden">
            <div className="rounded-2xl border border-stroke/60 bg-white/70 p-3 shadow-sm backdrop-blur-sm dark:border-dark-3 dark:bg-dark-2/40">
              <TenderSectionNavList
                items={tenderNavItems}
                activeId={activeSectionId}
                onSelect={scrollToTenderSection}
                variant="strip"
              />
            </div>
          </div>

          <TenderProcurementSection tender={tender} />
          {shouldShowLotsSection(tender) ? (
            <TenderLotsSection tender={tender} />
          ) : null}
          <TenderTimelineSection tender={tender} />

          {showLocation && <TenderLocationSection tender={tender} />}

          <TenderTranslationSection tender={tender} />
          {showDocumentsSection && (
            <TenderDocumentsSection
              tenderId={tenderId}
              documents={documents}
              isLoading={documentsLoading}
            />
          )}
          <TenderDescriptionSection tender={tender} />
          <TenderAiSummarySection tender={tender} />
        </div>

        <aside className="sticky top-24 z-10 mt-2 hidden min-w-0 self-start xl:block">
          <div className="shadow-theme-xs max-h-[calc(100vh-7rem)] overflow-y-auto rounded-2xl border border-stroke/60 bg-white/80 p-4 backdrop-blur-md dark:border-dark-3 dark:bg-dark-2/50">
            <TenderSectionNavList
              items={tenderNavItems}
              activeId={activeSectionId}
              onSelect={scrollToTenderSection}
              variant="rail"
            />
          </div>
        </aside>
      </div>
    </div>
  );
}
