import Link from "next/link";
import type { NoticeItem } from "@/lib/types";
import { formatDate } from "@/lib/content";
import { paperDay, stampOf, volumeLabel, volumeOf } from "@/lib/editions";
import { NamedIcon } from "@/components/icons";

export interface NoticeCardLabels {
  sampleBadge: string;
  numberPrefix: string;
  published: string;
  scheduled: string;
  /** "Volume {words}": the edition a notice ran in. */
  volumeTemplate?: string;
  /** First edition's date (Volume One), YYYY-MM-DD. */
  editionEpoch?: string;
  readLabel?: string;
}

export const DEFAULT_NOTICE_LABELS: NoticeCardLabels = {
  sampleBadge: "Sample Notice",
  numberPrefix: "No.",
  published: "Published",
  scheduled: "Scheduled",
  volumeTemplate: "Volume {roman} ({words})",
  editionEpoch: "2026-11-12",
  readLabel: "Read notice"
};

export function statusLabel(status: string, labels: NoticeCardLabels): string {
  return status === "scheduled" ? labels.scheduled : labels.published;
}

/** The paper day a notice was published: from its timestamp, else its date. */
export function noticeDay(notice: NoticeItem): string {
  if (notice.publishedAt) {
    const d = new Date(notice.publishedAt);
    if (!Number.isNaN(d.getTime())) return paperDay(d);
  }
  return notice.date;
}

/** "Volume One" for the edition a notice ran in, or "". */
export function noticeVolume(notice: NoticeItem, labels: NoticeCardLabels): string {
  const v = volumeOf(noticeDay(notice), labels.editionEpoch || DEFAULT_NOTICE_LABELS.editionEpoch!);
  return v ? volumeLabel(labels.volumeTemplate || DEFAULT_NOTICE_LABELS.volumeTemplate!, v) : "";
}

/**
 * One notice as a landscape card: the notice number on the left, the notice
 * (state, county, facility, title, summary) in the middle, and its
 * publication stamp (status, timestamp, volume) on the right, on an
 * unrolled paper scroll with a roll at each end. Lists stack the scrolls
 * one on another (the "cascade"). `look` only decides
 * whether the summary shows ("list" and "state" show it).
 */
export default function NoticeCard({
  notice,
  look,
  labels,
  hidden
}: {
  notice: NoticeItem;
  look: "grid" | "list" | "state";
  labels: NoticeCardLabels;
  hidden?: boolean;
}) {
  const kicker = [notice.stateName || notice.stateCode.toUpperCase(), notice.county, notice.facilityShort].filter(Boolean).join(" · ");
  const scheduled = notice.status === "scheduled";
  const stamp = !scheduled && notice.publishedAt ? stampOf(notice.publishedAt) : formatDate(notice.date);
  const volume = scheduled ? "" : noticeVolume(notice, labels);
  return (
    <Link className={`notice-landscape${scheduled ? " is-scheduled" : ""}`} href={`/notices/${notice.slug}`} style={hidden ? { display: "none" } : undefined}>
      {/* The notice is an unrolled scroll: a roll of paper at each end. */}
      <span className="nl-roll nl-roll-l" aria-hidden="true" />
      <span className="nl-roll nl-roll-r" aria-hidden="true" />
      <div className="nl-number">
        {notice.isSample ? (
          <span className="nl-sample">{labels.sampleBadge}</span>
        ) : (
          <>
            <span className="nl-no">{labels.numberPrefix}</span>
            <span className="nl-num">{notice.noticeNumber}</span>
          </>
        )}
      </div>
      <div className="nl-body">
        {kicker && <div className="nl-kicker">{kicker}</div>}
        <h4>{notice.title}</h4>
        {look !== "grid" && notice.summary && <p className="nl-summary">{notice.summary}</p>}
      </div>
      <div className="nl-stamp">
        <span className={`nl-status ${scheduled ? "pending" : "published"}`}>
          <span className="nl-dot" aria-hidden="true" />
          {statusLabel(notice.status, labels)}
        </span>
        {stamp && <time dateTime={notice.publishedAt || notice.date}>{stamp}</time>}
        {volume && <span className="nl-vol">{volume}</span>}
        <span className="nl-read">
          {labels.readLabel || DEFAULT_NOTICE_LABELS.readLabel}
          <NamedIcon name="chevron-right" size={14} />
        </span>
      </div>
    </Link>
  );
}
