/**
 * 时空命理学 · 家宅布局场强计算器（键共振 Destiny）
 * 依据案例 C-2026-001 公理一至八量化框架。
 *
 * 公式：
 *  ψ（意向强度） = 专注度 × 执行精密度 × 维护持续度
 *  φ（意向兑现率）= 0.5 + 0.5·ψ
 *  距离增益（公理四）= (1/2)^a：垂直同柱投影 a=−1→×2；同柱 a=0→×1；隔柱 a=1→×0.5；遥隔 a=2→×0.25
 *  E（有效场强）= 火源基数 × Π(空间因子) × φ
 *  物象浸染（公理三）总效系数 = 接触时长T × 距离增益
 *  临界：<0.6 不足 · 0.6~1.0 临界 · 1.0~1.5 黄金热值 · >1.5 过热
 * © 2026 Jason Li（泽森）
 */
(function (global) {
  "use strict";

  function psi(focus, precision, maintenance) {
    return +(focus * precision * maintenance).toFixed(4);
  }
  function phi(p) { return +(0.5 + 0.5 * p).toFixed(4); }
  function distGain(a) { return Math.pow(0.5, a); } // a=-1→2, 0→1, 1→0.5, 2→0.25
  function zone(E) {
    if (E < 0.6) return { key: "low", label: "能量不足", note: "布局基本无效" };
    if (E < 1.0) return { key: "edge", label: "临界态", note: "有感应但不稳定" };
    if (E <= 1.5) return { key: "gold", label: "黄金热值", note: "融寒温土，不燥不烈" };
    return { key: "hot", label: "过热", note: "可能烦躁/失眠，需泄火" };
  }
  function infiltration(T, a) { return +(T * distGain(a)).toFixed(3); }

  /**
   * @param d {base, factors:[{name,val}], intent:{focus,precision,maintenance}, items:[{name,T,a}]}
   */
  function compute(d) {
    const P = psi(d.intent.focus, d.intent.precision, d.intent.maintenance);
    const F = phi(P);
    const spaceProduct = (d.factors || []).reduce((m, f) => m * f.val, 1);
    const xi = +(d.base * spaceProduct).toFixed(4); // 空间增益 ξ（不含 φ）
    const E = +(xi * F).toFixed(3);
    const E0 = +(xi * 0.5).toFixed(3); // 无意向增益（φ 下限）
    const uplift = E0 > 0 ? +(((E - E0) / E0) * 100).toFixed(1) : 0;
    const items = (d.items || []).map(it => ({
      name: it.name, T: it.T, a: it.a, distGain: distGain(it.a), eff: infiltration(it.T, it.a),
    })).sort((x, y) => y.eff - x.eff);
    return { psi: P, phi: F, xi, E, E0, uplift, zone: zone(E), items };
  }

  // 案例 C-2026-001（寅午戌火局·恒温场）
  const C2026001 = {
    id: "C-2026-001",
    title: "寅午戌火局布设 · 申时抑郁戊土日主恒温场",
    diagnose: {
      F_ling: 0.6, base_strength: 1.2, shen_trigger: 1.3, crit_at_shen: 0.9,
      note: "亥月水旺土囚；亥子丑三会水；申时申金生水，戊土过临界。",
    },
    base: 1.0,
    factors: [
      { name: "同柱增益(车库直灌·垂直投影)", val: 2 },
      { name: "月令火旺值(丙午午火当令)", val: 0.9 },
      { name: "紫砂马转化率", val: 0.7 },
      { name: "红陶盆共振系数(黄土同频戊土)", val: 1.1 },
    ],
    intent: { focus: 1.0, precision: 0.95, maintenance: 0.9 },
    items: [
      { name: "红陶盆黄土(车库·床下同柱)", T: 1.0, a: -1 },
      { name: "红色长明灯(床下持续)", T: 1.0, a: -1 },
      { name: "柴檀木柜(每日经过)", T: 0.17, a: -1 },
      { name: "午方紫砂马(隔柱偶见)", T: 0.017, a: 2 },
    ],
    expect: { E: 1.286, E0: 0.693, psi: 0.855, phi: 0.9275, zone: "gold" },
  };

  function verifyC2026001() {
    const r = compute(C2026001);
    const near = (a, b) => Math.abs(a - b) <= 0.01;
    const checks = {
      psi: near(r.psi, C2026001.expect.psi),
      phi: near(r.phi, C2026001.expect.phi),
      E: near(r.E, C2026001.expect.E),
      E0: near(r.E0, C2026001.expect.E0),
      zone: r.zone.key === C2026001.expect.zone,
    };
    return { result: r, checks, pass: Object.values(checks).every(Boolean) };
  }

  global.KRLayout = { psi, phi, distGain, zone, infiltration, compute, C2026001, verifyC2026001 };
})(typeof window !== "undefined" ? window : globalThis);
