/* hub-data.jsx — dữ liệu + visual map cho Hub (plain JS, chạy node test được). */
(function () {
  const REF = Date.parse("2026-06-14T03:00:00Z");

  /* ---- avatar gradient theo tên ---- */
  const AV_PAIRS = [
    ["#3cc7f2", "#1a8ed4"], ["#34d399", "#0d9488"], ["#a78bfa", "#7c3aed"],
    ["#fbbf24", "#f59e0b"], ["#f87171", "#ef4444"], ["#60a5fa", "#2563eb"],
    ["#2dd4bf", "#0891b2"], ["#fb7185", "#e11d48"], ["#818cf8", "#4f46e5"], ["#f472b6", "#db2777"],
  ];
  function nameHash(s) { let h = 0; s = s || "?"; for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) >>> 0; return h; }
  function avatarBg(name) { const p = AV_PAIRS[nameHash(name) % AV_PAIRS.length]; return "linear-gradient(150deg, " + p[0] + ", " + p[1] + ")"; }
  function initials(name) {
    const p = (name || "?").trim().split(/\s+/);
    return (p.length > 1 ? p[p.length - 2][0] + p[p.length - 1][0] : p[0].slice(0, 2)).toUpperCase();
  }

  /* ---- loại sự kiện → icon / nhãn / màu ---- */
  const TYPE = {
    upload:   { ic: "upload",      label: "Nạp tệp",     c: "var(--bimup-2)" },
    status:   { ic: "flow",        label: "Trạng thái",  c: "var(--info)" },
    issue:    { ic: "flag",        label: "Issue",       c: "var(--danger)" },
    tx:       { ic: "send",        label: "Chuyển giao", c: "var(--arc)" },
    task:     { ic: "checkCircle", label: "Nhiệm vụ",    c: "var(--inf)" },
    prop:     { ic: "doc",         label: "Thuộc tính",  c: "var(--mep)" },
    folder:   { ic: "folder",      label: "Thư mục",     c: "var(--ink-3)" },
    template: { ic: "grid",        label: "Mẫu",         c: "var(--str)" },
    clash:    { ic: "burst",       label: "Va chạm",     c: "var(--danger)" },
    user:     { ic: "users",       label: "Thành viên",  c: "var(--ok)" },
    delete:   { ic: "ban",         label: "Xoá",         c: "var(--ink-3)" },
    other:    { ic: "dots",        label: "Khác",        c: "var(--ink-3)" },
  };
  const tint = (cvar) => "color-mix(in srgb, " + cvar + " 14%, transparent)";
  const DAY_LABEL = { today: "Hôm nay", yesterday: "Hôm qua", earlier: "Trước đó" };

  function relTime(iso) {
    const ms = REF - (Date.parse(iso) || REF);
    const h = ms / 3600000;
    if (h < 1) return "vừa xong";
    if (h < 24) return Math.round(h) + " giờ";
    if (h < 48) return "Hôm qua";
    return Math.round(h / 24) + " ngày trước";
  }
  function dayBucket(iso) {
    const d = (REF - (Date.parse(iso) || REF)) / 86400000;
    return d < 1 ? "today" : d < 2 ? "yesterday" : "earlier";
  }
  function mapAction(action) {
    const a = action || "";
    if (a.startsWith("upload")) return { verb: "đã nạp phiên bản mới", type: "upload" };
    if (a.startsWith("status:")) return { verb: "đổi trạng thái cho", type: "status" };
    if (a.startsWith("issue")) return { verb: "cập nhật issue", type: "issue" };
    if (a.startsWith("transmittal")) return { verb: "phát hành transmittal", type: "tx" };
    if (a.startsWith("folder")) return { verb: "tạo thư mục", type: "folder" };
    if (a.startsWith("clash")) return { verb: "xử lý va chạm", type: "clash" };
    if (a.startsWith("template")) return { verb: "cập nhật mẫu", type: "template" };
    if (a === "prop_create") return { verb: "thêm thuộc tính", type: "prop" };
    if (a === "prop_delete") return { verb: "xoá thuộc tính", type: "prop" };
    if (a === "prop_update" || a === "prop_edit") return { verb: "cập nhật thuộc tính", type: "prop" };
    if (a === "delete") return { verb: "đã xoá", type: "delete" };
    if (a === "register" || a === "login") return { verb: "tham gia hệ thống", type: "user" };
    return { verb: "có thao tác trên", type: "other" };
  }

  // build feed: audit thật (map) + nhiệm vụ demo, sắp giảm theo thời gian
  function buildActivities(audit, demoTasks, defaultProjectName) {
    const fromAudit = (audit || []).map((row, i) => {
      const m = mapAction(row.action);
      return {
        id: row.id ? ("au-" + row.id) : ("a" + i), type: m.type, actor: row.actor || "Hệ thống", actorId: row.actor_id || null, verb: m.verb,
        target: row.target_id || "", project: defaultProjectName || "Dự án", projectDot: "var(--bimup-2)",
        time: relTime(row.created_at), day: dayBucket(row.created_at), timeKey: Date.parse(row.created_at) || 0,
        reactions: { heart: 0 }, liked: false, comments: [],
      };
    });
    return fromAudit.concat(demoTasks || []).sort((x, y) => y.timeKey - x.timeKey);
  }

  const T = (mins) => REF - mins * 60000;
  // Bản chạy thử: KHÔNG có nhiệm vụ/thông báo giả — feed hoạt động chỉ lấy từ audit thật.
  const DEMO_TASKS = [];
  const DEMO_NOTIFICATIONS = [];

  Object.assign(window, { AV_PAIRS, nameHash, avatarBg, initials, TYPE, tint, DAY_LABEL, buildActivities, DEMO_TASKS, DEMO_NOTIFICATIONS });
})();
