'use client';

import type { ReadableContext } from '@/features/activities/readable';
import { getCompanyTimeline } from '@/features/companies/api.client';
import type { Company } from '@/features/companies/types';
import { PreviewSectionLook } from '@/features/contacts/ui/CollapsibleCard';
import { ActivityCards, WhenSeen } from '@/features/contacts/ui/ContactPreviewCards';
import { AttachmentsSidebarCard } from '@/features/files/ui/AttachmentsCard';
import { LinkedObjectCards } from '@/features/objects/ui/LinkedObjectCards';
import { CompanyAssociations } from './CompanyAssociations';

/**
 * B4 point 40: the company preview's cards after the design's "About this company" and
 * "Associations" (list-page.js:3623-3627): recent and upcoming activities, associated records
 * (contacts, related companies, deals, linked custom objects) and attachments. The record page's
 * and Contacts' preview's own cards, drawn as the design's preview sections, each loaded when it
 * comes into view. Out, as on Contacts (gap 28): the AI summary and items with no data source.
 */
export function CompanyPreviewCards({
  company,
  canEdit,
  ctx,
  refreshKey,
  onChanged,
  eager = false,
}: {
  company: Company;
  canEdit: boolean;
  ctx: ReadableContext;
  /** Bumped after a tool changed the company: the activity and attachment cards load again. */
  refreshKey: number;
  /** A card changed the company (a link added or removed): the panel and the list reload. */
  onChanged: () => Promise<void>;
  /** Mount every card at once (tests). */
  eager?: boolean;
}) {
  return (
    <PreviewSectionLook.Provider value>
      <WhenSeen eager={eager}>
        <ActivityCards
          contactId={company.id}
          ctx={ctx}
          refreshKey={refreshKey}
          load={getCompanyTimeline}
        />
      </WhenSeen>
      <WhenSeen eager={eager}>
        <CompanyAssociations
          company={company}
          canEdit={canEdit}
          onChanged={onChanged}
          look="preview"
        />
        {/* In the same wrapper: with no links it draws nothing, so it leaves no empty slot. */}
        <LinkedObjectCards
          anchorType="company"
          anchorId={company.id}
          anchorName={company.name}
          canEdit={canEdit}
          variant="contact"
          onlyLinked
        />
      </WhenSeen>
      <WhenSeen eager={eager}>
        <AttachmentsSidebarCard
          link={{ type: 'company', id: company.id }}
          canEdit={canEdit}
          refreshKey={refreshKey}
        />
      </WhenSeen>
    </PreviewSectionLook.Provider>
  );
}
