import { Check, CornerDownLeft, Hourglass, MessageSquare, Send, Tag, Ticket, X, type LucideIcon } from "lucide-react";
import { cn } from "cn";

import type { TimelineItem, TimelineTone } from "@/services/employee-requests/activity-timeline";

/**
 * The Activity list from the design: one row per event, a coloured node on a
 * vertical rule, the message that went with it as a quote, and a dashed node
 * for the step the request is waiting on.
 */

const ICONS: Record<TimelineItem["icon"], LucideIcon> = {
  Send,
  MessageSquare,
  CornerDownLeft,
  Check,
  X,
  Ticket,
  Hourglass,
  Tag,
};

const NODE: Record<TimelineTone, string> = {
  submitted: "bg-[#e6f0fb] text-[#1d4f91]",
  info: "bg-[var(--fra-info-solid)] text-white",
  reply: "bg-[var(--fra-info-bg)] text-[var(--fra-info-solid)]",
  approved: "bg-[var(--fra-approved-bg)] text-[var(--fra-approved-solid)]",
  rejected: "bg-[var(--fra-rejected-bg)] text-[var(--fra-rejected-solid)]",
  price: "bg-[var(--fra-pending-bg)] text-[var(--fra-pending)]",
  waiting: "border border-dashed border-[var(--fra-pending)] bg-[var(--fra-pending-bg)] text-[var(--fra-pending)]",
};

export function ActivityTimeline({ items }: { items: TimelineItem[] }) {
  return (
    <ol className="m-0 list-none p-0">
      {items.map((item, i) => {
        const Icon = ICONS[item.icon];
        const last = i === items.length - 1;
        return (
          <li key={item.key} className="relative flex gap-3 pb-3 last:pb-0">
            {!last && (
              <span aria-hidden className="absolute top-9 bottom-0 left-[17px] w-px bg-[var(--fra-border)]" />
            )}
            <span
              aria-hidden
              className={cn("relative inline-flex size-9 shrink-0 items-center justify-center rounded-full", NODE[item.tone])}
            >
              <Icon className="size-4" />
            </span>
            <div className="min-w-0 flex-1 pt-1">
              <strong className="block text-sm font-bold text-[var(--fra-heading)]">{item.title}</strong>
              {item.sub && <small className="block text-xs text-[var(--fra-muted)]">{item.sub}</small>}
              {item.quote && (
                <blockquote className="mt-2 rounded-[10px] bg-[var(--fra-surface-2)] px-3 py-2 text-sm text-[var(--fra-text)]">
                  {item.quote}
                </blockquote>
              )}
            </div>
          </li>
        );
      })}
    </ol>
  );
}
