// Rebuild every data file the page reads, from NOAA's own daily files.
//   node build.mjs [--cache DIR] [--out DIR]
// Downloads about 190 MB the first time (two stations, all of 2025, one padding day each side) into
// the cache (default ./.dew-cache in the current directory), then writes nights.json, findings.json
// and showcase.json into --out (default: the page's own data/ folder beside this pipeline).
import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url';
import { ensure } from './fetch.mjs';
import { loadRows, splitNights, summarise, stationNights, cloudEvents, thirds, validate, trace } from './analyze.mjs';
import { balance, balanceSheltered, WELLS_COVER } from '../engine.mjs';

const HERE = path.dirname(fileURLToPath(import.meta.url));
const arg = (k, d) => { const i = process.argv.indexOf(k); return i > 0 ? process.argv[i + 1] : d; };
const YEAR = 2025;

// All of 2025 plus one padding day either side, so the nights of 31 Dec 2024 and 31 Dec 2025 are whole.
export async function stationRows(st, log, CACHE) {
  const files = [...await ensure(st, 2024, [366], CACHE, log), ...await ensure(st, YEAR, Array.from({ length: 365 }, (_, i) => i + 1), CACHE, log), ...await ensure(st, 2026, [1], CACHE, log)];
  return loadRows(files.map(f => fs.readFileSync(f, 'utf8')));
}
const inYear = (n) => n.date.startsWith(String(YEAR));

// Run the thin-surface model through a night (view=1), integrating condensation to millimetres.
export function modelNight(tr, opts = {}) {
  let mm = 0, prev = null;
  for (let i = 0; i < tr.m.length; i++) {
    const b = balance({ dw: tr.dw[i], ta: tr.ta[i], rh: tr.rh[i], u10: tr.u[i], p: tr.p[i] }, opts);
    if (prev != null) mm += b.cond * (tr.m[i] - prev) * 60;   // kg/m2 == mm
    prev = tr.m[i];
  }
  return Math.round(mm * 1000) / 1000;
}

// Wells's handkerchief re-run on a night: mean deficits below the tower air, exposed vs sheltered (model).
export function handkerchief(tr) {
  let e = 0, s = 0, c = 0;
  for (let i = 0; i < tr.m.length; i++) {
    const inp = { dw: tr.dw[i], ta: tr.ta[i], rh: tr.rh[i], u10: tr.u[i], p: tr.p[i] };
    e += tr.ta[i] - balance(inp).ts; s += tr.ta[i] - balanceSheltered(inp).ts; c++;
  }
  const r = (x) => Math.round(x * 100) / 100;
  return { cover: Math.round(WELLS_COVER * 1000) / 1000, exposedBelowAir: r(e / c), shelteredBelowAir: r(s / c), shelteredWarmerBy: r((e - s) / c) };
}

// The showcase nights, each chosen by a rule stated here and on the page (not by eye).
export const RULES = [
  { id: 'dew', st: 'bon', label: 'Clear, calm and humid', rule: 'Bondville night with the most dew minutes among nights with sky emissivity < 0.83, mean 10 m wind < 2.9 m/s and mean air > 8 C', pick: N => N.filter(n => n.epsSky < 0.83 && n.wind < 2.9 && n.air > 8).sort((a, b) => b.dewMin - a.dewMin || a.date.localeCompare(b.date))[0] },
  { id: 'dry', st: 'bon', label: 'The coldest grass, and no dew', rule: 'Bondville night whose field ran furthest below the air on average', pick: N => [...N].sort((a, b) => a.surfMinusAir - b.surfMinusAir)[0] },
  { id: 'windy', st: 'bon', label: 'The same sky and air, with wind', rule: "the dew night's windy twin: the Bondville night with the strongest mean wind above 4.5 m/s whose sky emissivity is within 0.02 of the dew night's and whose dew-point depression is within 0.5 C of it, with the air above 8 C as on the dew night (so both are dew, not frost)", pick: (N, dew) => N.filter(n => n.air > 8 && Math.abs(n.epsSky - dew.epsSky) <= 0.02 && Math.abs((n.air - n.dewPoint) - (dew.air - dew.dewPoint)) <= 0.5 && n.wind > 4.5).sort((a, b) => b.wind - a.wind)[0] },
  { id: 'cloudy', st: 'bon', label: 'Overcast and calm', rule: 'Bondville night with the highest sky emissivity below 1 among nights with mean wind < 2.5 m/s and mean air > 8 C (emissivity above 1 means fog, or cloud warmer than the tower air)', pick: N => N.filter(n => n.wind < 2.5 && n.air > 8 && n.epsSky < 1).sort((a, b) => b.epsSky - a.epsSky)[0] },
  { id: 'cloudpass', st: 'bon', label: "Wells's cloud", rule: 'the Bondville night holding the strongest cloud event (sky IR up >= 30 W/m2, field up >= 2 C, air within 1.5 C, in 20 minutes) with the air above 5 C', pick: null },
  { id: 'frost', st: 'bon', label: 'Hoar frost', rule: 'Bondville night with the most minutes of possible deposition as ice', pick: N => [...N].sort((a, b) => b.frostMin - a.frostMin || a.date.localeCompare(b.date))[0] },
  { id: 'desert', st: 'dra', label: 'Desert: the coldest sky, and no dew', rule: 'Desert Rock night with the sky furthest below the air on average', pick: N => [...N].sort((a, b) => a.skyMinusAir - b.skyMinusAir)[0] },
  { id: 'desertdew', st: 'dra', label: 'Desert: the dewiest night of the year', rule: 'Desert Rock night with the most minutes of possible condensation', pick: N => [...N].sort((a, b) => b.condMin - a.condMin || a.date.localeCompare(b.date))[0] },
];

export async function build({ cache = path.resolve('.dew-cache'), log = (s) => process.stderr.write(s + '\n') } = {}) {
  const out = { nights: {}, findings: { source: 'NOAA GML SURFRAD daily 1-minute files, CC0 1.0', year: YEAR, stations: {} }, showcase: [] };
  const raw = {};
  for (const st of ['bon', 'dra']) {
    const { meta, rows } = await stationRows(st, log, cache);
    raw[st] = { rows, nights: splitNights(rows, st).filter(n => summarise(n) && inYear({ date: n.localDate })) };
    const N = stationNights(rows, st).filter(inYear);
    out.nights[st] = N;
    const grid = {};
    for (const eps of [0.95, 0.98, 0.99]) for (const margin of [-0.5, 0, 0.5]) {
      const G = stationNights(rows, st, { eps, margin }).filter(inYear);
      grid[`eps${eps}_m${margin}`] = { anyMin: G.filter(n => n.condMin > 0).length, hourPlus: G.filter(n => n.condMin >= 60).length };
    }
    const events = cloudEvents(rows, st).filter(e => e.local.startsWith(String(YEAR)) || e.utc.startsWith(String(YEAR)));
    const q = (a, p) => { const s = [...a].sort((x, y) => x - y); return s[Math.floor(p * (s.length - 1))]; };
    const clear = N.filter(n => n.epsSky < 0.8), over = N.filter(n => n.epsSky > 0.95);
    out.findings.stations[st] = {
      name: meta.name.trim(), lat: meta.lat, lon: meta.lon, elev: meta.elev, nights: N.length,
      condNightsAny: N.filter(n => n.condMin > 0).length, condNightsHour: N.filter(n => n.condMin >= 60).length,
      dewNights: N.filter(n => n.dewMin > 0).length, frostNights: N.filter(n => n.frostMin > 0).length,
      epsSkyMedian: q(N.map(n => n.epsSky), 0.5),
      clearNights: clear.length, clearSkyMinusAirMedian: clear.length ? q(clear.map(n => n.skyMinusAir), 0.5) : null,
      overcastNights: over.length, overcastSkyMinusAirMedian: over.length ? q(over.map(n => n.skyMinusAir), 0.5) : null,
      dewDepressionMedian: Math.round(q(N.map(n => n.air - n.dewPoint), 0.5) * 10) / 10,
      ceilingMax: Math.max(...N.map(n => n.ceilingMm)),
      ceilingMedianOnCondNights: q(N.filter(n => n.condMin >= 60).map(n => n.ceilingMm), 0.5),
      coldestFive: [...N].sort((a, b) => a.surfMinusAir - b.surfMinusAir).slice(0, 5).map(n => ({ date: n.date, surfMinusAir: n.surfMinusAir, condMin: n.condMin, dewDepression: Math.round((n.air - n.dewPoint) * 10) / 10 })),
      robustness: grid, thirds: thirds(N), thirdsMargin05: thirds(stationNights(rows, st, { margin: 0.5 }).filter(inYear)),
      validation: validate(rows.filter(r => r.year === YEAR), [0.5, 1, 1.5]),
      validationWatmuff: validate(rows.filter(r => r.year === YEAR), [1], 'watmuff'),
      cloudEvents: events.slice(0, 10),
      // Wells's order of events: on nights that reached deposition, did the field go cold first?
      precedingCold: (() => { const D = N.filter(n => n.firstDepMin != null && n.firstColdMin != null);
        return { nights: D.length, coldFirst: D.filter(n => n.firstColdMin < n.firstDepMin).length,
          depFirst: D.filter(n => n.firstDepMin < n.firstColdMin).length, same: D.filter(n => n.firstDepMin === n.firstColdMin).length,
          medianColdMin: D.length ? q(D.map(n => n.firstColdMin), 0.5) : null, medianDepMin: D.length ? q(D.map(n => n.firstDepMin), 0.5) : null,
          depWithoutColdNights: N.filter(n => n.firstDepMin != null && n.firstColdMin == null).length }; })(),
    };
    log(`${st}: ${N.length} nights`);
  }
  // showcase
  const evWarm = out.findings.stations.bon.cloudEvents.find(e => e.air > 5);
  for (const R of RULES) {
    let s;
    if (R.id === 'cloudpass') {
      const t = Date.parse(evWarm.utc + 'Z') / 60000;
      const n = raw.bon.nights.find(n => n.start <= t && t <= n.start + 24 * 60);
      s = { ...out.nights.bon.find(x => x.date === n.localDate) };
      s.event = evWarm;
    } else s = R.pick(out.nights[R.st], out.showcase.find(x => x.id === 'dew')?.summary);
    const night = raw[R.st].nights.find(n => n.localDate === s.date);
    const tr = trace(night);
    const shel = handkerchief(tr);
    out.showcase.push({ handkerchief: shel, id: R.id, station: R.st, label: R.label, rule: R.rule, summary: s, startUTC: new Date(night.start * 60000).toISOString().slice(0, 16),
      modelDewMm: modelNight(tr), trace: tr, ...(s.event ? { event: s.event } : {}) });
  }
  return out;
}
export const serialise = (out) => ({ 'nights.json': JSON.stringify(out.nights), 'findings.json': JSON.stringify(out.findings, null, 1), 'showcase.json': JSON.stringify(out.showcase) });

if (process.argv[1] === fileURLToPath(import.meta.url)) {
  const D = arg('--out', path.join(HERE, '..', 'data'));
  build({ cache: arg('--cache', path.resolve('.dew-cache')) }).then(out => {
    fs.mkdirSync(D, { recursive: true });
    for (const [f, txt] of Object.entries(serialise(out))) fs.writeFileSync(path.join(D, f), txt);
    process.stderr.write(`wrote nights.json, findings.json, showcase.json to ${D}\n`);
  }).catch(e => { console.error(e); process.exit(1); });
}
