import Link from 'next/link';
import type { DatePrefs } from '@/features/activities/readable';
import type { AssociationCellData } from '@/features/list-page/ui/AssociationCell';
import { LpIcon } from '@/features/list-page/ui/LpIcon';
import { Avatar } from '@/features/list-page/ui/RecordAvatar';
import { cn } from '@/lib/utils/cn';
import type { CompanyNextTask } from '../types';
import { nextTaskPill } from './companyBoard';

/** The record page's words when a read-only user can't log activity (CompanyDetailsView). */
export const NO_LOG_TITLE = "Your permissions don't allow you to log activity";

/** The design's preview quick actions this card offers (list-page.js:3619, icons :136, :138). */
const ACTIONS = [
  ['email', 'Email'],
  ['task', 'Task'],
] as const;

/**
 * B7, point 45: the row after a Companies board card's foot. An addition to the design's card
 * (HubSpot's board card); nothing the design draws moves. Linked contacts the person may see
 * (B5's `associations=contact`), the next open task, and the design's Email and Task quick
 * actions: links to the record page, which opens its own window (`?action=`), as the design's
 * preview does (list-page.js:3620) and master's company preview (RecordPreviewView).
 */
export function CompanyBoardExtras({
  id,
  name,
  contacts,
  nextTask,
  canLog,
  prefs,
}: {
  id: string;
  name: string;
  contacts?: AssociationCellData;
  nextTask?: CompanyNextTask | null;
  canLog: boolean;
  prefs: DatePrefs;
}) {
  const shown = contacts?.records.slice(0, 3) ?? [];
  const more = (contacts?.count ?? 0) - shown.length;
  const pill = nextTaskPill(nextTask, prefs);
  return (
    <>
      {shown.length ? (
        <span className="lp-bcard__avs">
          {shown.map((c) => {
            const label = c.name ?? 'Unnamed contact';
            return (
              <Link
                key={c.id}
                className="lp-bcard__av"
                href={`/contacts/${c.id}`}
                prefetch={false}
                title={label}
                aria-label={label}
              >
                <Avatar name={c.name ?? '?'} size="xs" />
              </Link>
            );
          })}
          {more > 0 ? (
            <span className="lp-bcard__more" title={`${more} more associated contacts`}>
              +{more}
            </span>
          ) : null}
        </span>
      ) : null}
      {pill ? (
        <span className={cn('badge', pill.tone && `badge--${pill.tone}`)} title={pill.title}>
          {pill.text}
        </span>
      ) : null}
      <span className="lp-bcard__acts">
        {ACTIONS.map(([action, label]) =>
          canLog ? (
            <Link
              key={action}
              className="lp-icon-btn lp-icon-btn--sm"
              href={`/companies/${id}?action=${action}`}
              prefetch={false}
              aria-label={`${label} ${name}`}
              title={label}
            >
              <LpIcon name={action} />
            </Link>
          ) : (
            <button
              key={action}
              type="button"
              className="lp-icon-btn lp-icon-btn--sm"
              aria-label={`${label} ${name}`}
              title={NO_LOG_TITLE}
              disabled
            >
              <LpIcon name={action} />
            </button>
          ),
        )}
      </span>
    </>
  );
}
