export type ArticleItem = {
  id: string;
  channel: string;
  author: {
    id: string;
    username: string;
    display_name: string;
    avatar_url: string;
  };
  title: string;
  cover_url: string | null;
  summary: string | null;
  tags: string[];
  like_count: number;
  comment_count: number;
  view_count: number;
  is_pinned: boolean;
  content_type: string;
  status: string;
  created_at: string;
  updated_at: string;
};

export type ArticleDetail = {
  id: string;
  channel: string;
  author: {
    id: string;
    username: string;
    display_name: string;
    avatar_url: string;
  };
  title: string;
  cover_url: string | null;
  summary: string | null;
  tags: string[];
  like_count: number;
  comment_count: number;
  view_count: number;
  is_pinned: boolean;
  content_type: string;
  status: string;
  created_at: string;
  updated_at: string;
  content: string;
};

export type ArticleComment = {
  id: string;
  article: string;
  parent: string | null;
  content: string;
  author: string;
  created_at: string;
  updated_at: string;
};

export function articleColor(name: string): string {
  let hash = 0;
  for (let i = 0; i < name.length; i++) {
    hash = name.charCodeAt(i) + ((hash << 5) - hash);
  }
  const hues = [
    "from-violet-500/20 to-purple-600/10",
    "from-fuchsia-500/20 to-pink-600/10",
    "from-indigo-500/20 to-blue-600/10",
    "from-emerald-500/20 to-teal-600/10",
    "from-amber-500/20 to-orange-600/10",
    "from-rose-500/20 to-red-600/10",
    "from-cyan-500/20 to-sky-600/10",
    "from-lime-500/20 to-green-600/10",
  ];
  return hues[Math.abs(hash) % hues.length];
}

export function articleInitial(name: string): string {
  return name
    .split(" ")
    .map((w) => w[0])
    .join("")
    .slice(0, 2)
    .toUpperCase();
}

export function formatRelativeTime(iso: string): string {
  const d = new Date(iso);
  const now = new Date();
  const diffMs = now.getTime() - d.getTime();
  const diffMin = Math.floor(diffMs / 60000);
  if (diffMin < 1) return "just now";
  if (diffMin < 60) return `${diffMin}m ago`;
  const diffHr = Math.floor(diffMin / 60);
  if (diffHr < 24) return `${diffHr}h ago`;
  const diffDay = Math.floor(diffHr / 24);
  if (diffDay < 7) return `${diffDay}d ago`;
  return d.toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
  });
}

export function formatCount(n: number): string {
  if (n >= 10000) return `${(n / 1000).toFixed(1)}k`;
  if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
  return String(n);
}
