// Original Gotanda guide data — fictional spots inspired by the area.
// Coordinates are realistic for Gotanda (35.626, 139.723).

const SPOTS = [
  {
    id: 'tomoshibi',
    cat: 'izakaya',
    name: { ja: '灯火 Tomoshibi', en: 'Tomoshibi Izakaya' },
    tag: { ja: '東口・路地裏', en: 'East Exit · Backstreet' },
    desc: {
      ja: '常連で賑わう小さなカウンター居酒屋。仕事帰りの一杯にぴったりの落ち着いた灯り。',
      en: 'A snug counter izakaya tucked behind the east exit. Warm lanterns and seasonal small plates.'
    },
    price: '¥¥',
    hours: { ja: '17:00–24:00 / 日休', en: '5pm–midnight · closed Sun' },
    walk: 4,
    rating: 4.6,
    tags: ['Sake', 'Counter', 'Late'],
    coords: [35.6262, 139.7235],
    address: { ja: '品川区東五反田 1-13', en: '1-13 Higashi-Gotanda' },
    img: 0,
  },
  {
    id: 'meguro-brew',
    cat: 'cafe',
    name: { ja: 'Meguro Brew', en: 'Meguro Brew' },
    tag: { ja: '目黒川沿い', en: 'Riverside' },
    desc: {
      ja: '川沿いの一軒家カフェ。朝7時オープン、出張前の打合せ需要に強い。',
      en: 'Riverfront cafe open from 7am — built for pre-meeting espresso and quiet calls.'
    },
    price: '¥¥',
    hours: { ja: '7:00–20:00', en: '7am–8pm' },
    walk: 6,
    rating: 4.4,
    tags: ['Wi-Fi', 'Power', 'Espresso'],
    coords: [35.6242, 139.7218],
    address: { ja: '品川区西五反田 2-8', en: '2-8 Nishi-Gotanda' },
    img: 1,
  },
  {
    id: 'sky-loft',
    cat: 'bar',
    name: { ja: 'Sky Loft 28', en: 'Sky Loft 28' },
    tag: { ja: '28F・夜景', en: '28F · City View' },
    desc: {
      ja: 'ホテル最上階のラウンジバー。羽田に降りる飛行機の灯りを眺めながらのナイトキャップ。',
      en: 'Top-floor hotel bar with a runway view of Haneda approach lights — ideal nightcap.'
    },
    price: '¥¥¥',
    hours: { ja: '18:00–02:00', en: '6pm–2am' },
    walk: 3,
    rating: 4.7,
    tags: ['View', 'Whisky', 'Date'],
    coords: [35.6275, 139.7240],
    address: { ja: '品川区東五反田 5-1', en: '5-1 Higashi-Gotanda' },
    img: 2,
  },
  {
    id: 'samidare',
    cat: 'ramen',
    name: { ja: '麺処 五月雨', en: 'Menjo Samidare' },
    tag: { ja: '濃厚煮干し', en: 'Niboshi Ramen' },
    desc: {
      ja: '深夜まで開く煮干しラーメン。出張帰りの〆に行列必至。',
      en: 'Late-night niboshi ramen counter — the unofficial last train of Gotanda.'
    },
    price: '¥',
    hours: { ja: '11:00–翌3:00', en: '11am–3am next' },
    walk: 2,
    rating: 4.5,
    tags: ['Late', 'Solo', 'Cash'],
    coords: [35.6256, 139.7225],
    address: { ja: '品川区東五反田 2-3', en: '2-3 Higashi-Gotanda' },
    img: 3,
  },
  {
    id: 'ichinosuke',
    cat: 'sushi',
    name: { ja: '鮨 一之介', en: 'Sushi Ichinosuke' },
    tag: { ja: 'おまかせ', en: 'Omakase' },
    desc: {
      ja: 'カウンター8席のおまかせ専門。海外クライアントの接待にも安心の英語対応。',
      en: 'Eight-seat omakase counter; English-friendly chef — a reliable client-dinner pick.'
    },
    price: '¥¥¥¥',
    hours: { ja: '18:00–23:00', en: '6pm–11pm' },
    walk: 5,
    rating: 4.8,
    tags: ['Omakase', 'EN-OK', 'Reserve'],
    coords: [35.6248, 139.7252],
    address: { ja: '品川区東五反田 1-25', en: '1-25 Higashi-Gotanda' },
    img: 4,
  },
  {
    id: 'morning-pier',
    cat: 'cafe',
    name: { ja: 'Morning Pier', en: 'Morning Pier' },
    tag: { ja: '朝活カフェ', en: 'Early Hours' },
    desc: {
      ja: '6時開店。朝食ボウルとフィルターコーヒー。電源・Wi-Fi完備。',
      en: 'Opens 6am. Breakfast bowls, filter coffee, ample power outlets and Wi-Fi.'
    },
    price: '¥¥',
    hours: { ja: '6:00–18:00', en: '6am–6pm' },
    walk: 1,
    rating: 4.3,
    tags: ['Breakfast', 'Wi-Fi', 'Quiet'],
    coords: [35.6259, 139.7228],
    address: { ja: '品川区東五反田 2-1', en: '2-1 Higashi-Gotanda' },
    img: 5,
  },
  {
    id: 'blue-side',
    cat: 'bar',
    name: { ja: 'Jazz Blue Side', en: 'Jazz Blue Side' },
    tag: { ja: '生演奏・地下', en: 'Live · Basement' },
    desc: {
      ja: '地下のジャズバー。週末はライブ、平日は静かに一杯。',
      en: 'Basement jazz bar — live sets on weekends, quiet drams midweek.'
    },
    price: '¥¥¥',
    hours: { ja: '19:00–02:00', en: '7pm–2am' },
    walk: 4,
    rating: 4.6,
    tags: ['Live', 'Whisky', 'Late'],
    coords: [35.6253, 139.7211],
    address: { ja: '品川区西五反田 1-9', en: '1-9 Nishi-Gotanda' },
    img: 6,
  },
  {
    id: 'stay-gotanda',
    cat: 'hotel',
    name: { ja: 'Stay Gotanda', en: 'Stay Gotanda' },
    tag: { ja: '駅徒歩2分', en: '2-min from station' },
    desc: {
      ja: 'ビジネス利用に特化したミッドレンジホテル。全室デスク広め、ランドリー24h。',
      en: 'Mid-range business hotel — wide desks, 24h laundry, late check-out option.'
    },
    price: '¥¥',
    hours: { ja: 'IN 15:00 / OUT 11:00', en: 'IN 3pm · OUT 11am' },
    walk: 2,
    rating: 4.2,
    tags: ['Desk', 'Laundry', 'Quiet'],
    coords: [35.6266, 139.7232],
    address: { ja: '品川区東五反田 2-6', en: '2-6 Higashi-Gotanda' },
    img: 7,
  },
  {
    id: 'atre',
    cat: 'shop',
    name: { ja: '駅ビル Gotanda Mall', en: 'Gotanda Mall' },
    tag: { ja: '駅直結', en: 'Station Connected' },
    desc: {
      ja: '駅直結の複合商業ビル。手土産・コスメ・地下フードホール。',
      en: 'Station-attached complex — souvenirs, cosmetics, basement food hall.'
    },
    price: '¥¥',
    hours: { ja: '10:00–22:00', en: '10am–10pm' },
    walk: 0,
    rating: 4.0,
    tags: ['Gifts', 'Food Hall', 'Daily'],
    coords: [35.6260, 139.7238],
    address: { ja: '品川区東五反田 1-2', en: '1-2 Higashi-Gotanda' },
    img: 8,
  },
  {
    id: 'ikedayama',
    cat: 'sight',
    name: { ja: '池田山公園', en: 'Ikedayama Park' },
    tag: { ja: '高台の和風庭園', en: 'Hilltop Garden' },
    desc: {
      ja: '旧岡山藩主邸跡の和風庭園。会議の合間の散歩におすすめ。',
      en: 'Quiet Japanese garden on the former Okayama lord residence — meeting-break walk.'
    },
    price: 'Free',
    hours: { ja: '9:00–17:00', en: '9am–5pm' },
    walk: 12,
    rating: 4.4,
    tags: ['Walk', 'Quiet', 'Garden'],
    coords: [35.6304, 139.7264],
    address: { ja: '品川区東五反田 5-4', en: '5-4 Higashi-Gotanda' },
    img: 9,
  },
  {
    id: 'hub',
    cat: 'work',
    name: { ja: 'Hub Gotanda', en: 'Hub Gotanda Coworking' },
    tag: { ja: 'コワーキング', en: 'Coworking' },
    desc: {
      ja: 'ドロップイン¥1,500/h。会議室・モニター・電話ブース完備。',
      en: 'Drop-in ¥1,500/h — meeting rooms, monitors, phone booths.'
    },
    price: '¥¥',
    hours: { ja: '8:00–22:00', en: '8am–10pm' },
    walk: 3,
    rating: 4.5,
    tags: ['Wi-Fi', 'Booth', 'Drop-in'],
    coords: [35.6271, 139.7220],
    address: { ja: '品川区西五反田 1-3', en: '1-3 Nishi-Gotanda' },
    img: 10,
  },
  {
    id: 'tcc',
    cat: 'sight',
    name: { ja: 'TOC ビル屋上', en: 'TOC Rooftop' },
    tag: { ja: '夕景スポット', en: 'Sunset Spot' },
    desc: {
      ja: '昭和の巨大商業ビル屋上。夕方の都心スカイラインが綺麗。',
      en: 'Massive Showa-era trade center rooftop — open city skyline at dusk.'
    },
    price: 'Free',
    hours: { ja: '10:00–18:00', en: '10am–6pm' },
    walk: 8,
    rating: 4.1,
    tags: ['View', 'Free', 'Photo'],
    coords: [35.6217, 139.7176],
    address: { ja: '品川区西五反田 7-22', en: '7-22 Nishi-Gotanda' },
    img: 11,
  },
];

const CATEGORIES = [
  { id: 'all', icon: '✦', label: { ja: 'すべて', en: 'All' } },
  { id: 'izakaya', icon: '提', label: { ja: '居酒屋', en: 'Izakaya' } },
  { id: 'ramen', icon: '麺', label: { ja: 'ラーメン', en: 'Ramen' } },
  { id: 'sushi', icon: '鮨', label: { ja: '寿司', en: 'Sushi' } },
  { id: 'cafe', icon: '☕', label: { ja: 'カフェ', en: 'Cafe' } },
  { id: 'bar', icon: '◉', label: { ja: 'バー', en: 'Bar' } },
  { id: 'hotel', icon: '泊', label: { ja: 'ホテル', en: 'Hotel' } },
  { id: 'shop', icon: '袋', label: { ja: 'ショップ', en: 'Shop' } },
  { id: 'sight', icon: '景', label: { ja: '名所', en: 'Sight' } },
  { id: 'work', icon: '⌘', label: { ja: 'ワーク', en: 'Work' } },
];

const EVENTS = [
  {
    date: '05.18',
    day: { ja: '日', en: 'Sun' },
    name: { ja: '目黒川ナイトマーケット', en: 'Meguro River Night Market' },
    where: { ja: '目黒川河岸', en: 'Meguro Riverside' },
    tag: { ja: '屋外', en: 'Outdoor' },
  },
  {
    date: '05.24',
    day: { ja: '土', en: 'Sat' },
    name: { ja: 'Gotanda Jazz Crawl', en: 'Gotanda Jazz Crawl' },
    where: { ja: '駅周辺6会場', en: '6 venues near station' },
    tag: { ja: '音楽', en: 'Music' },
  },
  {
    date: '06.07',
    day: { ja: '土', en: 'Sat' },
    name: { ja: '商店街朝市', en: 'Shotengai Morning Market' },
    where: { ja: '東口商店街', en: 'East Exit Shotengai' },
    tag: { ja: '朝', en: 'Morning' },
  },
  {
    date: '06.15',
    day: { ja: '日', en: 'Sun' },
    name: { ja: '出張者向け街歩きツアー', en: 'Business Visitor Walking Tour' },
    where: { ja: '駅西口集合', en: 'West Exit Meet' },
    tag: { ja: 'ツアー', en: 'Tour' },
  },
];

const NEWS = [
  {
    cat: { ja: '新店', en: 'New' },
    title: {
      ja: '駅前再開発エリアにルーフトップバーがオープン',
      en: 'New rooftop bar opens in station redevelopment block',
    },
    excerpt: {
      ja: '東口の再開発タワー28階に、滑走路の灯りまで見渡せるラウンジが登場。',
      en: 'The 28F lounge in the east-exit tower offers views all the way to Haneda runway lights.',
    },
    when: { ja: '2日前', en: '2 days ago' },
  },
  {
    cat: { ja: '更新', en: 'Update' },
    title: {
      ja: '目黒川沿いに新しい朝活カフェが追加されました',
      en: 'New early-hours cafe added along the Meguro River',
    },
    excerpt: {
      ja: '6時開店、フィルターコーヒーと朝食ボウル。Wi-Fiと電源完備。',
      en: 'Opens 6am, with filter coffee, breakfast bowls, Wi-Fi and power.',
    },
    when: { ja: '5日前', en: '5 days ago' },
  },
  {
    cat: { ja: 'ガイド', en: 'Guide' },
    title: {
      ja: '出張者のための五反田・夜の過ごし方10選',
      en: '10 ways to spend the night in Gotanda — for the visiting business traveler',
    },
    excerpt: {
      ja: '会食後の一杯から、深夜の〆ラーメンまで網羅したナイトガイド。',
      en: 'From the post-dinner dram to the 2am ramen — a complete after-hours guide.',
    },
    when: { ja: '1週間前', en: '1 week ago' },
  },
];

const HOOD_STATS = [
  { label: { ja: '駅利用者数', en: 'Daily riders' }, value: '143K', sub: { ja: '/日', en: '/day' } },
  { label: { ja: '飲食店数', en: 'Eateries' }, value: '1,200+', sub: { ja: '駅周辺', en: 'near station' } },
  { label: { ja: '羽田まで', en: 'To Haneda' }, value: '28', sub: { ja: '分', en: 'min' } },
  { label: { ja: '東京駅まで', en: 'To Tokyo St.' }, value: '17', sub: { ja: '分', en: 'min' } },
];

const I18N = {
  brand: { ja: 'GOTANDA / 五反田', en: 'GOTANDA' },
  tagline: {
    ja: '出張者のための、夜まで動ける街ガイド',
    en: 'A night-friendly guide for the visiting business traveler',
  },
  hero_title: {
    ja: '深夜2時の街、\n仕事の合間の散歩。',
    en: 'A city that runs past 2am.\nA park for between meetings.',
  },
  hero_sub: {
    ja: '駅から徒歩10分以内の、出張中に役立つ12スポット。',
    en: 'Twelve spots within a 10-minute walk of the station — picked for the visiting professional.',
  },
  search_ph: { ja: '店名・ジャンルで検索', en: 'Search by name or category' },
  nav: {
    home: { ja: 'ホーム', en: 'Home' },
    spots: { ja: 'スポット', en: 'Spots' },
    map: { ja: 'マップ', en: 'Map' },
    saved: { ja: '保存', en: 'Saved' },
  },
  sec_spots: { ja: 'おすすめスポット', en: 'Recommended Spots' },
  sec_tonight: { ja: '今夜の五反田', en: 'Tonight in Gotanda' },
  sec_map: { ja: 'マップ＆アクセス', en: 'Map & Access' },
  sec_events: { ja: 'イベント', en: 'Events' },
  sec_news: { ja: 'ニュース', en: 'News' },
  sec_area: { ja: 'エリアについて', en: 'About the Area' },
  see_all: { ja: 'すべて見る', en: 'See all' },
  walk_min: { ja: '分', en: 'min' },
  reserve: { ja: '地図で見る', en: 'Open in Maps' },
  save: { ja: '保存', en: 'Save' },
  saved_lbl: { ja: '保存済み', en: 'Saved' },
  no_saved: { ja: 'まだ保存したスポットはありません', en: 'No saved spots yet' },
  no_results: { ja: '該当するスポットがありません', en: 'No spots match your filters' },
  area_blurb: {
    ja: '五反田は山手線の南西、品川と目黒の間にある駅の街。昼はオフィス街、夜は飲食街、週末は目黒川沿いの散歩道に変わる、出張者にとって扱いやすい都市の縮図。',
    en: 'Gotanda sits on the southwest curve of the Yamanote, between Shinagawa and Meguro. Office district by day, food district by night, riverside stroll on weekends — a compact city sampler for the visiting traveler.',
  },
  footer: {
    ja: '本サイトは架空の街ガイドプロトタイプです。',
    en: 'This site is a fictional city-guide prototype.',
  },
};

// Initial synchronous payload — used as a fallback for offline / standalone
// builds, and as the seed before /api/* finishes loading on the live site.
window.__GG = { SPOTS, CATEGORIES, EVENTS, NEWS, HOOD_STATS, I18N };

// Loader the app boots through. Fetches the canonical lists from /api/*
// (backed by D1), and falls back to the inline arrays above when offline,
// when running from file://, or when /api is unreachable.
window.__GG_LOAD = async function loadGG() {
  try {
    const [spotsRes, catsRes] = await Promise.all([
      fetch('/api/spots',      { cache: 'no-cache' }),
      fetch('/api/categories', { cache: 'no-cache' }),
    ]);
    if (!spotsRes.ok || !catsRes.ok) throw new Error('api status not ok');
    const apiSpots = await spotsRes.json();
    const apiCategories = await catsRes.json();
    if (!Array.isArray(apiSpots) || !Array.isArray(apiCategories)) {
      throw new Error('api shape unexpected');
    }
    // The API response is authoritative. An empty `spots` array means the
    // operator deleted everything on purpose — don't second-guess them by
    // showing the inline fallback. We only fall back if the fetch itself
    // failed (caught below).
    window.__GG = {
      SPOTS: apiSpots,
      CATEGORIES: apiCategories.length ? apiCategories : CATEGORIES,
      EVENTS, NEWS, HOOD_STATS, I18N,
    };
    console.log('[gg] loaded from /api', apiSpots.length, 'spots,', apiCategories.length, 'categories');
  } catch (e) {
    console.warn('[gg] /api unavailable — using inline fallback', e?.message || e);
  }
  return window.__GG;
};
