"use client";
/** Shared bits for the Module 11 social inbox pages. */
import { MessageCircle, MessageSquareText, Star } from "lucide-react";
import { useEffect, useState } from "react";
import { Badge, cx } from "./ui";

export const CHANNEL_LABEL: Record<string, string> = {
  whatsapp: "WhatsApp", messenger: "Messenger", instagram: "Instagram DM", sms: "SMS", email: "Email",
  facebook_comment: "Facebook", instagram_comment: "Instagram", youtube: "YouTube", tiktok: "TikTok", linkedin: "LinkedIn", google_review: "Google review",
};
export const SOCIAL_TONE: Record<string, string> = {
  whatsapp: "green", email: "blue", sms: "amber", messenger: "violet", instagram: "red",
  facebook_comment: "blue", instagram_comment: "red", youtube: "red", tiktok: "gray", linkedin: "blue", google_review: "amber",
};
export const PLATFORM_LABEL: Record<string, string> = { facebook: "Facebook Page", instagram: "Instagram", youtube: "YouTube", google_business: "Google Business Profile", tiktok: "TikTok", linkedin: "LinkedIn" };
export const INTENTS = ["price_inquiry", "location_inquiry", "booking", "interested", "support", "complaint", "thanks", "spam", "legal", "other"];
export const intentLabel = (i?: string | null) => (i ? i.replace(/_/g, " ") : "—");

export function KindIcon({ kind, className }: { kind: string; className?: string }) {
  const c = cx("h-3.5 w-3.5", className);
  return kind === "review" ? <Star className={c} /> : kind === "comment_thread" ? <MessageSquareText className={c} /> : <MessageCircle className={c} />;
}

export function ChannelBadge({ channel, className }: { channel: string; className?: string }) {
  return <Badge tone={SOCIAL_TONE[channel] ?? "gray"} className={className}>{CHANNEL_LABEL[channel] ?? channel}</Badge>;
}

export function Stars({ rating, size = 14 }: { rating: number; size?: number }) {
  return (
    <span className="inline-flex" aria-label={`${rating} out of 5 stars`}>
      {[1, 2, 3, 4, 5].map((i) => <Star key={i} style={{ width: size, height: size }} className={i <= rating ? "fill-warn text-warn" : "text-ink-3/40"} />)}
    </span>
  );
}

export function SentimentDot({ s }: { s?: string | null }) {
  if (!s) return null;
  return <span title={`Sentiment: ${s}`} className={cx("inline-block h-2 w-2 shrink-0 rounded-full", s === "negative" ? "bg-bad" : s === "positive" ? "bg-ok" : "bg-ink-3/40")} />;
}

/** Live SLA countdown chip (SOC-071). */
export function SlaChip({ due, breached, responded, compact }: { due?: string | null; breached?: string | null; responded?: string | null; compact?: boolean }) {
  const [, tick] = useState(0);
  useEffect(() => { const i = setInterval(() => tick((n) => n + 1), 30000); return () => clearInterval(i); }, []);
  if (responded || (!due && !breached)) return null;
  const ms = due ? new Date(due).getTime() - Date.now() : -1;
  const over = !!breached || ms < 0;
  const m = Math.abs(Math.round(ms / 60000));
  const t = m < 60 ? `${m}m` : m < 1440 ? `${Math.floor(m / 60)}h${m % 60 ? ` ${m % 60}m` : ""}` : `${Math.round(m / 1440)}d`;
  return <span title={over ? "SLA breached" : "Time left to first response"} className={cx("inline-flex shrink-0 items-center rounded-full px-1.5 py-px text-[10.5px] font-semibold tabular-nums", over ? "bg-bad/12 text-bad" : ms < 15 * 60000 ? "bg-warn/15 text-warn" : "bg-ink/[.06] text-ink-2")}>{over ? (compact ? `+${t}` : `SLA +${t}`) : compact ? t : `SLA ${t}`}</span>;
}

export const fmtSecs = (s: number | null | undefined) => {
  if (s == null) return "—";
  if (s < 60) return `${Math.round(s)}s`;
  if (s < 3600) return `${Math.round(s / 60)}m`;
  if (s < 86400) return `${(s / 3600).toFixed(1)}h`;
  return `${(s / 86400).toFixed(1)}d`;
};
