/* ============================================================
   meta.jsx — assembles the rich instrument-card fields (location,
   owner, calibration cert, PM …) from real sheet data, with
   sensible fallbacks (dept default location/owner) when a field
   was never entered. No synthesized/fake values — a field that
   hasn't been filled in shows as blank/unknown, not a guess.

   7 ส.ค. 2569: calibrationResults() was deleted from here. It invented
   measured values and deviations from a hash of the instrument code.
   Nothing rendered them — the real ISO 15189 §6.5 review (calreview.jsx)
   uses numbers staff type in — but a fabricated calibration reading has no
   business sitting one line away from a quality record. Real results live
   in the CalReviews tab, never in code.
   ============================================================ */
(function () {
  function instrumentMeta(inst) {
    const m = window.DEPT_META[inst.dept] || {};

    // calibration interval from cal→exp, else assume 365d
    let interval = 365;
    if (inst.cal && inst.exp) {
      interval = Math.max(30, Math.round((new Date(inst.exp) - new Date(inst.cal)) / 86400000));
    }
    let pmCycleMonths = interval >= 330 ? 12 : 6;
    if (inst.pmPerYear && Number(inst.pmPerYear) > 0) pmCycleMonths = Math.round(12 / Number(inst.pmPerYear));

    // ---- ISO 15189 master-list fields ----
    const isAnalyzer = /อัตโนมัติ|วิเคราะห์|analyz/i.test(`${inst.type || ''} ${inst.typeName || ''}`) || /AUT|BSC|PCR|SEQ|CBC/.test((inst.code || '').split('-')[2] || '');

    return {
      location: inst.location || (window.deptLoc ? window.deptLoc(inst.dept) : `${m.th} (${m.short})`),
      owner: inst.owner || (window.DEPT_OWNER || {})[inst.dept] || null,
      // no status ever recorded → say so, don't guess "ใช้งานได้"
      op: inst.opKey ? { key: inst.opKey, label: { up: 'ใช้งานได้', down: 'งดใช้ชั่วคราว', repair: 'รอซ่อมบำรุง' }[inst.opKey] } : { key: 'unknown', label: 'ไม่ระบุ' },
      intake: inst.intake || null,
      provider: inst.provider || null,
      certNo: inst.certNo || null,
      calResult: inst.calResult || null,
      interval,
      pmCycle: `ทุก ${pmCycleMonths} เดือน`,
      pmCycleMonths,
      pmLast: inst.pmLast || null,
      pmNext: inst.pmNext || null,
      // ISO 15189 master-list — real data only; left blank until entered
      // (no synthesized demo fallbacks). Fill from the sheet/edit drawer.
      vendor: inst.vendor || null,
      vendorContact: inst.vendorContact || null,
      commissionDate: inst.commissionDate || null,
      acceptance: inst.acceptance || null,
      condition: inst.condition || null,
      firmware: inst.firmware || null,
      warrantyEnd: inst.warrantyEnd || null,
      serviceContract: inst.serviceContract || null,
      manualRef: inst.manualRef || null,
      lifecycle: inst.lifecycle || 'active',
      decommissionDate: inst.decommissionDate || null,
      decommissionReason: inst.decommissionReason || null,
      replacedBy: inst.replacedBy || null,
    };
  }

  window.instrumentMeta = instrumentMeta;
})();
