export type MentionSurface = "post" | "reply" | "quote";

import type { MentionInboxItem } from "../api/haixun";

export type ThreadsMentionHit = {
  id: string;
  text?: string;
  username?: string;
  permalink?: string;
  timestamp?: string;
  media_type?: string;
  is_reply?: boolean;
  is_quote_post?: boolean;
  has_replies?: boolean;
  root_post_id?: string;
  parent_id?: string;
  thread_post_text?: string;
  parent_reply_text?: string;
  reply_ready?: boolean;
  reply_ready_msg?: string;
  synced_at?: number;
};

export function mentionInboxItemToHit(item: MentionInboxItem): ThreadsMentionHit {
  return {
    id: item.media_id,
    text: item.text,
    username: item.author_username,
    permalink: item.permalink,
    timestamp: item.timestamp,
    media_type: item.media_type,
    is_reply: item.is_reply,
    is_quote_post: item.is_quote_post,
    has_replies: item.has_replies,
    root_post_id: item.root_post_id,
    parent_id: item.parent_id,
    thread_post_text: item.thread_post_text,
    parent_reply_text: item.parent_reply_text,
    reply_ready: item.reply_ready,
    reply_ready_msg: item.reply_ready_msg,
    synced_at: item.synced_at
  };
}

export function parseMentionHits(data: string): ThreadsMentionHit[] {
  const trimmed = data.trim();
  if (!trimmed) return [];
  try {
    const parsed = JSON.parse(trimmed) as unknown;
    if (!Array.isArray(parsed)) return [];
    const hits: ThreadsMentionHit[] = [];
    for (const item of parsed) {
      if (!item || typeof item !== "object") continue;
      const row = item as Record<string, unknown>;
      const id = String(row.id || "").trim();
      if (!id) continue;
      hits.push({
        id,
        text: typeof row.text === "string" ? row.text : undefined,
        username: typeof row.username === "string" ? row.username : undefined,
        permalink: typeof row.permalink === "string" ? row.permalink : undefined,
        timestamp: typeof row.timestamp === "string" ? row.timestamp : undefined,
        media_type: typeof row.media_type === "string" ? row.media_type : undefined,
        is_reply: row.is_reply === true,
        is_quote_post: row.is_quote_post === true,
        has_replies: row.has_replies === true,
        root_post_id: typeof row.root_post_id === "string" ? row.root_post_id : undefined,
        parent_id: typeof row.parent_id === "string" ? row.parent_id : undefined
      });
    }
    return hits;
  } catch {
    return [];
  }
}

export function mentionSurface(hit: ThreadsMentionHit): MentionSurface {
  if (hit.is_quote_post) return "quote";
  if (hit.is_reply) return "reply";
  return "post";
}

export function mentionSurfaceLabel(surface: MentionSurface): string {
  if (surface === "quote") return "引用貼文";
  if (surface === "reply") return "留言串";
  return "貼文";
}

export function mentionSurfaceHint(surface: MentionSurface): string {
  if (surface === "quote") return "對方在引用貼文中 @ 你";
  if (surface === "reply") return "對方在留言中 @ 你";
  return "對方在貼文中 @ 你";
}

export function mentionReplyTargetId(hit: ThreadsMentionHit): string {
  return hit.id;
}

export function mentionThreadRootId(hit: ThreadsMentionHit): string {
  return hit.root_post_id || hit.id;
}

export type PublishReplyResult = {
  media_id?: string;
  permalink?: string;
};

export function parsePublishReplyResult(data: string): PublishReplyResult | null {
  const trimmed = data.trim();
  if (!trimmed) return null;
  try {
    const row = JSON.parse(trimmed) as Record<string, unknown>;
    return {
      media_id: typeof row.media_id === "string" ? row.media_id : undefined,
      permalink: typeof row.permalink === "string" ? row.permalink : undefined
    };
  } catch {
    return null;
  }
}

export function formatMentionTimestamp(value?: string) {
  if (!value) return "";
  const date = new Date(value);
  if (!Number.isFinite(date.getTime())) return value;
  return date.toLocaleString("zh-TW", {
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit"
  });
}