/**
 * Frontend-only template specs.
 * Variants:
 *  - news
 *  - breaking (news urgent)
 *  - finance (news finance)
 *  - market
 *  - friends
 *  - events
 */
export const TEMPLATE_SPECS = {
  news: {
    mini_spec: {
      size: { w: 240, h: 96 },
      background: { type: "gradient", value: "newsBlue" },
      radius: 18,
      layers: [
        { id: "badge", type: "chip", x: 12, y: 10, text: "NEWS", style: "chip.news" },
        { id: "title", type: "text", x: 12, y: 34, w: 216, h: 42, bind: "data.headline", style: "text.title", maxLines: 2 },
        { id: "meta", type: "text", x: 12, y: 76, w: 216, h: 16, bind: "data.source", style: "text.meta", maxLines: 1 },
      ],
    },
    full_spec: {
      size: { w: 360, h: 260 },
      background: { type: "solid", value: "surface" },
      radius: 22,
      layers: [
        { id: "badge", type: "chip", x: 16, y: 12, text: "NEWS", style: "chip.news" },
        { id: "title", type: "text", x: 16, y: 52, w: 328, h: 72, bind: "data.headline", style: "text.h1", maxLines: 3 },
        { id: "summary", type: "text", x: 16, y: 132, w: 328, h: 78, bind: "data.summary", style: "text.body", maxLines: 4 },
      ],
    },
  },

  breaking: {
    mini_spec: {
      size: { w: 240, h: 96 },
      background: { type: "gradient", value: "breakingRed" },
      radius: 18,
      layers: [
        { id: "badge", type: "chip", x: 12, y: 10, text: "BREAKING", style: "chip.danger" },
        { id: "title", type: "text", x: 12, y: 34, w: 216, h: 42, bind: "data.headline", style: "text.title", maxLines: 2 },
        { id: "meta", type: "text", x: 12, y: 76, w: 216, h: 16, bind: "data.source", style: "text.meta", maxLines: 1 },
      ],
    },
    full_spec: {
      size: { w: 360, h: 260 },
      background: { type: "gradient", value: "breakingRed" },
      radius: 22,
      layers: [
        { id: "badge", type: "chip", x: 16, y: 12, text: "BREAKING", style: "chip.danger" },
        { id: "title", type: "text", x: 16, y: 52, w: 328, h: 72, bind: "data.headline", style: "text.h1", maxLines: 3 },
        { id: "summary", type: "text", x: 16, y: 132, w: 328, h: 78, bind: "data.summary", style: "text.body", maxLines: 4 },
      ],
    },
  },

  finance: {
    mini_spec: {
      size: { w: 240, h: 96 },
      background: { type: "gradient", value: "financeTeal" },
      radius: 18,
      layers: [
        { id: "badge", type: "chip", x: 12, y: 10, text: "FINANCE", style: "chip.news" },
        { id: "title", type: "text", x: 12, y: 34, w: 216, h: 42, bind: "data.headline", style: "text.title", maxLines: 2 },
        { id: "meta", type: "text", x: 12, y: 76, w: 216, h: 16, bind: "data.source", style: "text.meta", maxLines: 1 },
      ],
    },
    full_spec: {
      size: { w: 360, h: 260 },
      background: { type: "gradient", value: "financeTeal" },
      radius: 22,
      layers: [
        { id: "badge", type: "chip", x: 16, y: 12, text: "FINANCE", style: "chip.news" },
        { id: "title", type: "text", x: 16, y: 52, w: 328, h: 72, bind: "data.headline", style: "text.h1", maxLines: 3 },
        { id: "summary", type: "text", x: 16, y: 132, w: 328, h: 78, bind: "data.summary", style: "text.body", maxLines: 4 },
      ],
    },
  },

  market: {
    mini_spec: {
      size: { w: 240, h: 96 },
      background: { type: "gradient", value: "marketGold" },
      radius: 18,
      layers: [
        { id: "badge", type: "chip", x: 12, y: 10, text: "MARKET", style: "chip.news" },
        { id: "title", type: "text", x: 12, y: 34, w: 216, h: 42, bind: "data.headline", style: "text.title", maxLines: 2 },
        { id: "meta", type: "text", x: 12, y: 76, w: 216, h: 16, bind: "data.source", style: "text.meta", maxLines: 1 },
      ],
    },
    full_spec: {
      size: { w: 360, h: 260 },
      background: { type: "gradient", value: "marketGold" },
      radius: 22,
      layers: [
        { id: "badge", type: "chip", x: 16, y: 12, text: "MARKET", style: "chip.news" },
        { id: "title", type: "text", x: 16, y: 52, w: 328, h: 72, bind: "data.headline", style: "text.h1", maxLines: 3 },
        { id: "summary", type: "text", x: 16, y: 132, w: 328, h: 78, bind: "data.summary", style: "text.body", maxLines: 4 },
      ],
    },
  },

  friends: {
    mini_spec: {
      size: { w: 240, h: 96 },
      background: { type: "gradient", value: "friendsGreen" },
      radius: 18,
      layers: [
        { id: "badge", type: "chip", x: 12, y: 10, text: "FRIENDS", style: "chip.news" },
        { id: "title", type: "text", x: 12, y: 34, w: 216, h: 42, bind: "data.headline", style: "text.title", maxLines: 2 },
        { id: "meta", type: "text", x: 12, y: 76, w: 216, h: 16, bind: "data.source", style: "text.meta", maxLines: 1 },
      ],
    },
    full_spec: {
      size: { w: 360, h: 260 },
      background: { type: "gradient", value: "friendsGreen" },
      radius: 22,
      layers: [
        { id: "badge", type: "chip", x: 16, y: 12, text: "FRIENDS", style: "chip.news" },
        { id: "title", type: "text", x: 16, y: 52, w: 328, h: 72, bind: "data.headline", style: "text.h1", maxLines: 3 },
        { id: "summary", type: "text", x: 16, y: 132, w: 328, h: 78, bind: "data.summary", style: "text.body", maxLines: 4 },
      ],
    },
  },

  events: {
    mini_spec: {
      size: { w: 240, h: 96 },
      background: { type: "gradient", value: "eventsPurple" },
      radius: 18,
      layers: [
        { id: "badge", type: "chip", x: 12, y: 10, text: "EVENTS", style: "chip.news" },
        { id: "title", type: "text", x: 12, y: 34, w: 216, h: 42, bind: "data.headline", style: "text.title", maxLines: 2 },
        { id: "meta", type: "text", x: 12, y: 76, w: 216, h: 16, bind: "data.source", style: "text.meta", maxLines: 1 },
      ],
    },
    full_spec: {
      size: { w: 360, h: 260 },
      background: { type: "gradient", value: "eventsPurple" },
      radius: 22,
      layers: [
        { id: "badge", type: "chip", x: 16, y: 12, text: "EVENTS", style: "chip.news" },
        { id: "title", type: "text", x: 16, y: 52, w: 328, h: 72, bind: "data.headline", style: "text.h1", maxLines: 3 },
        { id: "summary", type: "text", x: 16, y: 132, w: 328, h: 78, bind: "data.summary", style: "text.body", maxLines: 4 },
      ],
    },
  },
};

function parseDateAny(raw) {
  if (!raw) return null;
  if (raw instanceof Date) return raw;
  const s = String(raw).trim();
  if (!s) return null;
  // handle "YYYY-MM-DD HH:mm:ss"
  const fixed = s.includes(" ") && !s.includes("T") ? s.replace(" ", "T") : s;
  const d = new Date(fixed);
  return Number.isNaN(d.getTime()) ? null : d;
}

function isBreaking(post) {
  if (post?.breaking === true || post?.is_breaking === true) return true;

  const t = (post?.title || "").toString().toLowerCase();
  if (t.includes("breaking") || t.includes("urgent") || t.includes("alerte")) return true;

  const d = parseDateAny(post?.created_at || post?.CreatedAt || post?.createdAt);
  if (!d) return false;
  const mins = (Date.now() - d.getTime()) / 60000;
  return mins >= 0 && mins <= 45;
}

function normCat(post) {
  const c = (post?.category || post?.Category || "").toString().toUpperCase();
  const sub = (post?.sub_category || post?.subCategory || "").toString().toLowerCase();

  if (c === "MARKET") return "market";
  if (c === "FRIENDS") return "friends";
  if (c === "EVENT" || c === "EVENTS") return "events";

  if (c === "NEWS" && (sub.includes("finan") || sub.includes("finance"))) return "finance";
  if (c === "NEWS" && isBreaking(post)) return "breaking";

  return "news";
}

export function getTemplateKeyForPost(post) {
  return normCat(post);
}

export function getTemplateSpecForPost(post, variant /* "mini"|"full" */) {
  const key = getTemplateKeyForPost(post);
  const t = TEMPLATE_SPECS[key] || TEMPLATE_SPECS.news;
  return variant === "full" ? t.full_spec : t.mini_spec;
}

export function adaptPostToTemplateData(post) {
  const headline = post?.title || "Untitled";
  const author = post?.author ? `by ${post.author}` : "";
  const sub = post?.sub_category ? String(post.sub_category) : "";
  const source = author || sub || "";
  const summary = post?.snippet || post?.body || "";
  const url = post?.url || "";
  const image = post?.image_large || post?.image_small || post?.image || post?.media_url || "";
  return { headline, source, summary, url, image };
}
