import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

/** Vietnamese-friendly relative time, e.g. "12 phút trước" */
export function relativeTime(date: string | Date | number): string {
  const d = typeof date === "object" ? date : new Date(date);
  const diff = Math.floor((Date.now() - d.getTime()) / 1000);
  if (diff < 30) return "Vừa xong";
  if (diff < 60) return `${diff} giây trước`;
  if (diff < 3600) return `${Math.floor(diff / 60)} phút trước`;
  if (diff < 86400) return `${Math.floor(diff / 3600)} giờ trước`;
  if (diff < 86400 * 7) return `${Math.floor(diff / 86400)} ngày trước`;
  return d.toLocaleDateString("vi-VN", { day: "2-digit", month: "2-digit", year: "numeric" });
}

/** Read time estimate (Vietnamese ~200wpm) */
export function readTime(html: string): number {
  const text = html.replace(/<[^>]*>/g, " ").trim();
  const words = text.split(/\s+/).filter(Boolean).length;
  return Math.max(1, Math.round(words / 200));
}

/** Strip HTML to plain text */
export function stripHtml(html: string, max = 200): string {
  const text = html
    .replace(/<[^>]*>/g, " ")
    .replace(/&nbsp;/g, " ")
    .replace(/&amp;/g, "&")
    .replace(/&lt;/g, "<")
    .replace(/&gt;/g, ">")
    .replace(/&quot;/g, '"')
    .replace(/\s+/g, " ")
    .trim();
  if (text.length <= max) return text;
  return text.slice(0, max).replace(/\s+\S*$/, "") + "…";
}

/** Pick category accent class from category slug */
export function categoryAccent(slug: string): {
  tagClass: string;
  color: string;
  label: string;
} {
  const map: Record<string, { tagClass: string; color: string; label: string }> = {
    crypto: { tagClass: "tag-crypto", color: "var(--cat-crypto)", label: "Crypto" },
    btc: { tagClass: "tag-crypto", color: "var(--cat-crypto)", label: "BTC" },
    altcoin: { tagClass: "tag-crypto", color: "var(--cat-crypto)", label: "Altcoin" },
    airdrop: { tagClass: "tag-crypto", color: "var(--cat-crypto)", label: "Airdrop" },
    "chung-khoan": { tagClass: "tag-stock", color: "var(--cat-stock)", label: "Chứng khoán" },
    "chung-khoan-viet-nam": { tagClass: "tag-stock", color: "var(--cat-stock)", label: "CK Việt Nam" },
    "chung-khoan-my": { tagClass: "tag-stock", color: "var(--cat-stock)", label: "CK Mỹ" },
    vang: { tagClass: "tag-gold", color: "var(--cat-gold)", label: "Vàng" },
    "thi-truong-vang": { tagClass: "tag-gold", color: "var(--cat-gold)", label: "Thị trường Vàng" },
    "thi-truong-bac": { tagClass: "tag-gold", color: "var(--cat-gold)", label: "Bạc" },
    forex: { tagClass: "tag-forex", color: "var(--cat-forex)", label: "Forex" },
    "ngoai-hoi": { tagClass: "tag-forex", color: "var(--cat-forex)", label: "Ngoại hối" },
    "thi-truong-dau-mo": { tagClass: "tag-forex", color: "var(--cat-forex)", label: "Dầu" },
    "phan-tich": { tagClass: "tag-analysis", color: "var(--cat-analysis)", label: "Phân tích" },
    "tin-nong": { tagClass: "tag-breaking", color: "var(--cat-breaking)", label: "Tin Nóng" },
    "cap-nhat-nhanh": { tagClass: "tag-breaking", color: "var(--cat-breaking)", label: "Cập nhật" },
    "dau-tu": { tagClass: "tag-analysis", color: "var(--cat-analysis)", label: "Đầu tư" },
    "kinh-nghiem-dau-tu": { tagClass: "tag-analysis", color: "var(--cat-analysis)", label: "Kinh nghiệm" },
    "thi-truong": { tagClass: "tag-analysis", color: "var(--cat-analysis)", label: "Thị trường" },
    "tin-tuc-tai-chinh": { tagClass: "tag-analysis", color: "var(--cat-analysis)", label: "Tin tức" },
  };
  return (
    map[slug] ?? { tagClass: "tag-analysis", color: "var(--cat-analysis)", label: slug }
  );
}
