// Arcaid Guild — mock data + helpers (exported to window for cross-file use)

const GUILD_USERS = {
  arcaid_official: {
    handle: "arcaid_official", name: "ArcAid 運営",
    bio: "テスラオーナーのための保険代理店 ArcAid 公式アカウント。補償・修理・イベントの最新情報をお届けします。",
    avatar: "#149a48", initials: "AA", official: true,
    cars: ["Model S", "Model X"], website: "arcaid.co.jp",
    following: 12, followers: 4820,
  },
  yuki_m3: {
    handle: "yuki_m3", name: "Yuki",
    bio: "2023 Model 3 オーナー。週末はもっぱらドライブ。納車2年目です。",
    avatar: "#2f6df0", initials: "YK", cars: ["Model 3"], website: "",
    following: 84, followers: 132,
  },
  haru_y: {
    handle: "haru_y", name: "はる",
    bio: "Model Y Long Range。子育て中ファミリーEV勢。充電スポット情報が好き。",
    avatar: "#bc7155", initials: "HR", cars: ["Model Y"], website: "",
    following: 41, followers: 67,
  },
  kenji_sx: {
    handle: "kenji_sx", name: "Kenji",
    bio: "Model S Plaid / Model X。FSD ベータ追っかけ。ガジェット好き。",
    avatar: "#0c4a30", initials: "KJ", cars: ["Model S", "Model X"], website: "kenji.dev",
    following: 203, followers: 511,
  },
  mai_cy: {
    handle: "mai_cy", name: "Mai",
    bio: "Cybertruck 納車待ち。今は Model Y に乗ってます。",
    avatar: "#f5a623", initials: "MA", cars: ["Model Y", "Cybertruck"], website: "",
    following: 19, followers: 28,
  },
};

const GUILD_CATEGORIES = [
  { key: "main", label: "メイン", color: "#149a48" },
  { key: "hardware", label: "ハードウェア", color: "#2f6df0" },
  { key: "software", label: "ソフトウェア", color: "#bc7155" },
];

const GUILD_TOPICS = [
  {
    id: "t1", pinned: true, category: "main", author: "arcaid_official",
    title: "【お知らせ】プレミアム補償が全プラン標準付帯になりました",
    body: "いつもArcAid Guildをご利用いただきありがとうございます。\n\nこの度、ガラス破損・いたずら・落書きを等級に影響なくカバーする「プレミアム補償」を、追加保険料なしで全プランに標準付帯いたしました。\n\n対象や上限など詳しくは限定コラムでも解説しています。ご質問はこのトピックへどうぞ。 #お知らせ #プレミアム補償",
    createdAt: Date.now() - 1000 * 60 * 60 * 6,
    views: 1284, likes: 142, bookmarks: 88, images: [],
  },
  {
    id: "t2", category: "hardware", author: "haru_y",
    title: "飛び石でフロントガラスにヒビ…交換した方いますか？",
    body: "高速でフロントガラスに飛び石を受けてしまい、20cmくらいのヒビが入りました。リペアで済むのか交換になるのか、経験ある方いたら費用感や日数を教えてほしいです。 #ガラス #修理",
    createdAt: Date.now() - 1000 * 60 * 60 * 28,
    views: 642, likes: 37, bookmarks: 24, images: ["#cfe6d8"],
  },
  {
    id: "t3", category: "software", author: "kenji_sx",
    title: "最新アップデートでナビの充電プランが賢くなった件",
    body: "今朝のソフトウェアアップデート後、ナビの充電ルート提案がかなり改善されてる気がします。到着時のバッテリー残量の精度が上がった印象。みなさんの環境ではどうですか？ #ソフトウェア #アップデート",
    createdAt: Date.now() - 1000 * 60 * 60 * 50,
    views: 988, likes: 76, bookmarks: 41, images: [],
  },
  {
    id: "t4", category: "main", author: "yuki_m3",
    title: "納車2年目、初めての保険更新で気をつけたことまとめ",
    body: "ちょうど更新タイミングだったので、見直したポイントを共有します。車両保険の金額設定、運転者範囲、特約の整理あたり。代理店に相談したら等級の引き継ぎもスムーズでした。 #保険 #更新",
    createdAt: Date.now() - 1000 * 60 * 60 * 96,
    views: 1530, likes: 119, bookmarks: 96, images: [],
  },
  {
    id: "t5", category: "hardware", author: "mai_cy",
    title: "純正と社外、フロアマットどっち派？",
    body: "汚れ対策にオールウェザーマットを検討中。純正は高いけどフィット感が良さそう。社外で十分という声もあり迷ってます。おすすめあれば！ #アクセサリ",
    createdAt: Date.now() - 1000 * 60 * 60 * 120,
    views: 410, likes: 22, bookmarks: 9, images: [],
  },
  {
    id: "t6", category: "software", author: "kenji_sx",
    title: "FSDの挙動、交差点の右折がスムーズになった",
    body: "ここ最近のアップデートで交差点の右折判断が自然になってきました。まだ手は添えてますが進化を感じます。 #FSD #ソフトウェア",
    createdAt: Date.now() - 1000 * 60 * 60 * 150,
    views: 1120, likes: 88, bookmarks: 52, images: [],
  },
];

const GUILD_COMMENTS = {
  t1: [
    { id: 1, author: "yuki_m3", body: "ありがたいです！ガラス周りは地味に不安だったので助かります。", createdAt: Date.now() - 1000 * 60 * 60 * 5, likes: 12, bookmarks: 1 },
    { id: 2, author: "haru_y", body: "落書きもカバーなんですね。立体駐車場使うので安心。", createdAt: Date.now() - 1000 * 60 * 60 * 4, likes: 8, bookmarks: 0 },
    { id: 3, author: "arcaid_official", body: ">>2 はい、いたずら・落書きも対象です。年度ごとに1回が上限となります。", createdAt: Date.now() - 1000 * 60 * 60 * 3, likes: 15, bookmarks: 2 },
  ],
  t2: [
    { id: 1, author: "yuki_m3", body: "20cmだと交換が無難かもです。私はリペアで失敗して結局交換になりました…", createdAt: Date.now() - 1000 * 60 * 60 * 26, likes: 9, bookmarks: 0 },
    { id: 2, author: "kenji_sx", body: "ヒビの位置が視界にかかるなら交換推奨。提携工場だと純正ガラスで対応してくれました。", createdAt: Date.now() - 1000 * 60 * 60 * 24, likes: 14, bookmarks: 3 },
    { id: 3, author: "arcaid_official", body: ">>1 プレミアム補償の対象になるケースです。お見積りは事故受付からどうぞ。", createdAt: Date.now() - 1000 * 60 * 60 * 22, likes: 11, bookmarks: 1 },
  ],
  t3: [
    { id: 1, author: "haru_y", body: "うちも残量予測の精度上がりました！地味に嬉しい改善。", createdAt: Date.now() - 1000 * 60 * 60 * 48, likes: 6, bookmarks: 0 },
  ],
  t4: [
    { id: 1, author: "mai_cy", body: "更新前にこれ読みたかった…運転者範囲は見落としがちですよね。", createdAt: Date.now() - 1000 * 60 * 60 * 90, likes: 7, bookmarks: 1 },
    { id: 2, author: "kenji_sx", body: "車両保険の金額設定、中古だと特に悩みます。参考になりました。", createdAt: Date.now() - 1000 * 60 * 60 * 88, likes: 5, bookmarks: 0 },
  ],
  t5: [],
  t6: [
    { id: 1, author: "yuki_m3", body: "右折スムーズになりましたよね。夜間の認識も良くなった気がします。", createdAt: Date.now() - 1000 * 60 * 60 * 148, likes: 4, bookmarks: 0 },
  ],
};

const GUILD_COLUMNS = [
  {
    id: "c1", title: "テスラのガラス破損、知っておきたい補償と修理の流れ",
    excerpt: "飛び石によるヒビから全面交換まで。費用の目安と、等級に影響させない補償の使い方を解説します。",
    hero: "#cfe6d8", date: Date.now() - 1000 * 60 * 60 * 24 * 3, likes: 96, bookmarks: 71,
    body: [
      { type: "p", text: "テスラのフロントガラスは大きく、飛び石によるダメージが意外と起こりやすいパーツです。本コラムでは、ヒビの程度ごとの対応と、ArcAidプレミアム補償の活用方法を整理します。" },
      { type: "h", text: "リペアで済むケース" },
      { type: "p", text: "ヒビが小さく、運転視界にかからない位置であればリペアで対応できることがあります。早めの処置がポイントです。" },
      { type: "img", color: "#bcd9c6" },
      { type: "h", text: "交換が必要なケース" },
      { type: "p", text: "視界にかかる位置や20cmを超える大きなヒビは、安全のため交換が推奨されます。提携工場では純正相当ガラスでの対応が可能です。" },
      { type: "h", text: "等級に影響させない使い方" },
      { type: "p", text: "プレミアム補償はガラス破損を等級に影響なくカバーします。保険年度ごとに1回が上限となるため、計画的にご利用ください。" },
    ],
  },
  {
    id: "c2", title: "代理店型 vs ダイレクト型、テスラオーナーの選び方",
    excerpt: "事故対応や相談のしやすさは何が違う？テスラ特有の事情から、向いている人を整理しました。",
    hero: "#dbe7f5", date: Date.now() - 1000 * 60 * 60 * 24 * 9, likes: 58, bookmarks: 44,
    body: [
      { type: "p", text: "保険料だけでは見えにくい「対応の質」。テスラに詳しい担当者が伴走する代理店型の価値を、具体例で解説します。" },
      { type: "h", text: "事故対応のスピード" },
      { type: "p", text: "提携修理ネットワークとの連携により、入庫から復旧までの段取りが速いのが代理店型の強みです。" },
    ],
  },
  {
    id: "c3", title: "充電環境を整える。自宅充電のはじめ方",
    excerpt: "200V設置の流れ、費用、賃貸での選択肢まで。これから始める人のための基礎ガイド。",
    hero: "#f3e6cf", date: Date.now() - 1000 * 60 * 60 * 24 * 16, likes: 72, bookmarks: 63,
    body: [
      { type: "p", text: "自宅充電があるとEVライフは一気に快適になります。設置の基本と注意点をまとめました。" },
      { type: "h", text: "戸建ての場合" },
      { type: "p", text: "分電盤から専用回路を引く工事が一般的です。見積りは複数社で比較しましょう。" },
    ],
  },
];

const GUILD_NOTIFICATIONS = [
  { id: "n1", type: "comment", actor: "arcaid_official", topic: "t2", excerpt: "プレミアム補償の対象になるケースです。お見積りは…", createdAt: Date.now() - 1000 * 60 * 60 * 2 },
  { id: "n2", type: "like", actor: "kenji_sx", topic: "t4", createdAt: Date.now() - 1000 * 60 * 60 * 5 },
  { id: "n3", type: "comment", actor: "mai_cy", topic: "t4", excerpt: "更新前にこれ読みたかった…運転者範囲は見落としがち…", createdAt: Date.now() - 1000 * 60 * 60 * 9 },
  { id: "n4", type: "like", actor: "haru_y", topic: "t4", createdAt: Date.now() - 1000 * 60 * 60 * 20 },
];

const GUILD_TAGS = ["お知らせ", "プレミアム補償", "ガラス", "修理", "ソフトウェア", "アップデート", "保険", "更新", "FSD", "アクセサリ", "充電"];

const GUILD_ME = "yuki_m3"; // current logged-in member

function guildRelTime(ts) {
  const d = Date.now() - ts;
  const min = Math.floor(d / 60000);
  if (min < 1) return "たった今";
  if (min < 60) return min + "分前";
  const h = Math.floor(min / 60);
  if (h < 24) return h + "時間前";
  const day = Math.floor(h / 24);
  if (day < 30) return day + "日前";
  const mo = Math.floor(day / 30);
  if (mo < 12) return mo + "ヶ月前";
  return Math.floor(mo / 12) + "年前";
}

function guildDateStr(ts) {
  const dt = new Date(ts);
  return dt.getFullYear() + "." + String(dt.getMonth() + 1).padStart(2, "0") + "." + String(dt.getDate()).padStart(2, "0");
}

function guildCat(key) {
  return GUILD_CATEGORIES.find((c) => c.key === key) || GUILD_CATEGORIES[0];
}

// auto-extract #tags from body text
function guildExtractTags(text) {
  const out = [];
  const re = /#([^\s#　]+)/g;
  let m;
  while ((m = re.exec(text)) !== null) out.push(m[1]);
  return out;
}

Object.assign(window, {
  GUILD_USERS, GUILD_CATEGORIES, GUILD_TOPICS, GUILD_COMMENTS, GUILD_COLUMNS,
  GUILD_NOTIFICATIONS, GUILD_TAGS, GUILD_ME,
  guildRelTime, guildDateStr, guildCat, guildExtractTags,
});
