// Triplicity Astrology — production lookup tables
// COPY-PASTED FROM technical handoff doc. Do not modify values.
// Component code should import these instead of defining its own constants.

// 3-letter → full sign name (API returns the abbr)
const SIGN_MAP = {
  Ari:'Aries', Tau:'Taurus', Gem:'Gemini', Can:'Cancer', Leo:'Leo', Vir:'Virgo',
  Lib:'Libra', Sco:'Scorpio', Sag:'Sagittarius', Cap:'Capricorn', Aqu:'Aquarius', Pis:'Pisces'
};

// Full sign → Traditional Chinese
const SIGN_ZH = {
  Aries:'牡羊座', Taurus:'金牛座', Gemini:'雙子座', Cancer:'巨蟹座',
  Leo:'獅子座', Virgo:'處女座', Libra:'天秤座', Scorpio:'天蠍座',
  Sagittarius:'射手座', Capricorn:'摩羯座', Aquarius:'水瓶座', Pisces:'雙魚座'
};

// Planet → Traditional Chinese
const PLANET_ZH = {
  Sun:'太陽', Moon:'月亮', Mercury:'水星', Venus:'金星', Mars:'火星',
  Jupiter:'木星', Saturn:'土星', Uranus:'天王星', Neptune:'海王星',
  Pluto:'冥王星', Chiron:'凱龍星', 'North Node':'北交點', 'South Node':'南交點',
  Ascendant:'上升點', MC:'天頂'
};

// Planet → Unicode glyph (text presentation forced via \uFE0E in wheel renderer)
const PLANET_GLYPH = {
  Sun:'☉', Moon:'☽', Mercury:'☿', Venus:'♀', Mars:'♂', Jupiter:'♃',
  Saturn:'♄', Uranus:'♅', Neptune:'♆', Pluto:'♇', Chiron:'⚷',
  'North Node':'☊', 'South Node':'☋', Ascendant:'AC', MC:'MC'
};

// Planet → display colour
const PLANET_COLOR = {
  Sun:'#ba7517', Moon:'#534ab7', Mercury:'#3b6d11', Venus:'#993556',
  Mars:'#a32d2d', Jupiter:'#185fa5', Saturn:'#5f5e5a', Uranus:'#0f6e56',
  Neptune:'#185fa5', Pluto:'#3c3489', Chiron:'#854f0b',
  'North Node':'#639922', 'South Node':'#888780',
  Ascendant:'#854f0b', MC:'#854f0b'
};

// Sign → element
const SIGN_ELEM = {
  Aries:'fire', Leo:'fire', Sagittarius:'fire',
  Taurus:'earth', Virgo:'earth', Capricorn:'earth',
  Gemini:'air', Libra:'air', Aquarius:'air',
  Cancer:'water', Scorpio:'water', Pisces:'water'
};

// Element → colour (for sign ring sectors in wheel)
const ELEM_COLOR = { fire:'#a32d2d', earth:'#3b6d11', air:'#0c447c', water:'#3c3489' };

// Planets displayed on the wheel (MC excluded from wheel glyphs)
const DISPLAY = [
  'Sun','Moon','Mercury','Venus','Mars','Jupiter','Saturn',
  'Uranus','Neptune','Pluto','Chiron','North Node','South Node'
];

// House string → number (legacy API format)
const H2N = {
  First_House:1, Second_House:2, Third_House:3, Fourth_House:4,
  Fifth_House:5, Sixth_House:6, Seventh_House:7, Eighth_House:8,
  Ninth_House:9, Tenth_House:10, Eleventh_House:11, Twelfth_House:12
};

// House taglines (adult — index by house number 1..12)
const HOUSE_TAGLINE = {
  1:'自我形象 · 個人世界觀',
  2:'財富價值 · 金錢資源觀',
  3:'溝通方式 · 學習與思維',
  4:'家庭根源 · 內在安全感',
  5:'創造表達 · 愛情與玩樂',
  6:'日常工作 · 健康與生活習慣',
  7:'伴侶關係 · 合作與對應模式',
  8:'親密連結 · 共享資源與轉化',
  9:'人生信念 · 高等知識與遠方探索',
  10:'事業方向 · 社會角色與成就',
  11:'朋友圈子 · 社群理想與未來願景',
  12:'潛意識世界 · 隱藏課題與內在療癒',
};

// Planet role labels (adult — shown under planet name in cards)
const PLANET_ROLE = {
  Sun:'核心自我 · 生命方向',
  Moon:'情感需求 · 安全感',
  Mercury:'思維方式 · 溝通風格',
  Venus:'愛情觀 · 審美與金錢',
  Mars:'行動力 · 慾望與驅動',
  Jupiter:'擴張 · 機遇與信念',
  Saturn:'責任 · 人生功課',
  Uranus:'突破 · 革新（世代行星）',
  Neptune:'靈感 · 靈性（世代行星）',
  Pluto:'轉化 · 深層蛻變（世代行星）',
  Chiron:'療癒 · 傷痛與禮物',
  'North Node':'靈魂方向 · 今生課題',
  'South Node':'前世習性 · 舒適圈',
};

// ── Interpretation accessors (mirror /interpretations.json shape) ──
// File is FLAT with snake_case keys: planets_in_signs.sun_libra, signs_in_houses.house_1_gemini,
// stamps.elem_fire_dom, soul_signatures.sig_libra, quadrants.quad_bottom, stelliums.stellium_sign
// All return the same shape as in the JSON (with .text field), or null if missing.

const _slug = (s) => String(s || '').toLowerCase().replace(/\s+/g, '_');

// planet ∈ {Sun, Moon, ..., North Node, ...}, sign ∈ {Aries, Taurus, ...}
function getPlanetInSign(planet, sign){
  return window.INTERPRETATIONS?.planets_in_signs?.[`${_slug(planet)}_${_slug(sign)}`] || null;
}
// Ascendant has no planets_in_signs entry — its interpretation comes from
// signs_in_houses[house_1_<sign>]. This helper transparently handles it.
function getAscendantInSign(sign){
  return getSignInHouse(1, sign);
}
function getSignInHouse(houseNum, sign){
  return window.INTERPRETATIONS?.signs_in_houses?.[`house_${houseNum}_${_slug(sign)}`] || null;
}
// stamps: element keys 'fire'/'earth'/'air'/'water', modality keys 'card'/'fix'/'mut',
// status 'dom' (dominant) or 'miss' (missing/缺失). Returns { type, status, label, text }.
function getElementStamp(elem, status){
  return window.INTERPRETATIONS?.stamps?.[`elem_${_slug(elem)}_${status}`] || null;
}
function getModalityStamp(mode, status){
  // mode in {'card','fix','mut'}
  return window.INTERPRETATIONS?.stamps?.[`mode_${mode}_${status}`] || null;
}
// soul_signatures: keyed by sign (sig_aries..sig_pisces). Each sign IS an element×modality
// combo, so derive the sign from dominant element × dominant modality.
const _ELEM_MODE_TO_SIGN = {
  'fire-card':'aries',     'fire-fix':'leo',       'fire-mut':'sagittarius',
  'earth-card':'capricorn','earth-fix':'taurus',   'earth-mut':'virgo',
  'air-card':'libra',      'air-fix':'aquarius',   'air-mut':'gemini',
  'water-card':'cancer',   'water-fix':'scorpio',  'water-mut':'pisces',
};
function getSoulSignatureBySign(sign){
  return window.INTERPRETATIONS?.soul_signatures?.[`sig_${_slug(sign)}`] || null;
}
function getSoulSignatureByDominance(elem, mode){
  const sign = _ELEM_MODE_TO_SIGN[`${elem}-${mode}`];
  return sign ? getSoulSignatureBySign(sign) : null;
}
// quadrants: keyed by direction ∈ {top, bottom, left, right}. Returns { direction, title, text }.
function getQuadrant(direction){
  return window.INTERPRETATIONS?.quadrants?.[`quad_${direction}`] || null;
}
// stelliums: keyed by kind ∈ {'sign', 'house'}.
function getStellium(kind){
  return window.INTERPRETATIONS?.stelliums?.[`stellium_${kind}`] || null;
}
// chart ruler (守護小天使): prefer _child key, fall back to base (per guide §6)
function getChartRulerPlanet(planet){
  const I = window.INTERPRETATIONS?.chart_ruler_planets; if (!I) return null;
  const k = `chart_ruler_${_slug(planet)}`;
  return I[`${k}_child`] || I[k] || null;
}
function getChartRulerHouse(house){
  const I = window.INTERPRETATIONS?.chart_ruler_houses; if (!I) return null;
  const k = `chart_ruler_house_${house}`;
  return I[`${k}_child`] || I[k] || null;
}

// ── Tier helpers ──
// paidLevel comes from Worker KV response (data.paid_tier).
// Never trust the URL `tri=` param for rendering — only use paidLevel.
//   null   → free
//   'sign' → $38 (planet-in-sign interpretations)
//   'full' → $68 (sign + synthesis + houses)
const isPaidLevel  = (lvl) => !!lvl && lvl !== 'free';
const isSignLevel  = (lvl) => lvl === 'sign' || lvl === 'full';
const isFullLevel  = (lvl) => lvl === 'full';

// Export everything to window so other Babel <script> blocks can use them.
Object.assign(window, {
  SIGN_MAP, SIGN_ZH, PLANET_ZH, PLANET_GLYPH, PLANET_COLOR,
  SIGN_ELEM, ELEM_COLOR, DISPLAY, H2N,
  HOUSE_TAGLINE, PLANET_ROLE,
  isPaidLevel, isSignLevel, isFullLevel,
  // accessors
  getPlanetInSign, getAscendantInSign, getSignInHouse,
  getElementStamp, getModalityStamp,
  getSoulSignatureBySign, getSoulSignatureByDominance,
  getQuadrant, getStellium,
  getChartRulerPlanet, getChartRulerHouse,
});
