// Standalone verifier, also distributed with each frozen dataset. Node 24+, no packages or network.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { createHash } from "node:crypto";
import { pathToFileURL } from "node:url";

const round = (x, places) => Math.round(x * 10 ** places) / 10 ** places;
const close = (actual, expected, message) => {
  assert(Number.isFinite(actual) && Number.isFinite(expected), `${message}: non-finite value`);
  assert(Math.abs(actual - expected) < 1e-9, `${message}: ${actual} != ${expected}`);
};
const fraction = (value, name) => assert(Number.isFinite(value) && value >= 0 && value < 1, name);
const positive = (value, name) => assert(Number.isFinite(value) && value > 0, name);
export const sha256 = (value) => createHash("sha256").update(value).digest("hex");

export function verifyDownload(manifestPath, expectedId) {
  assert(/^[a-f0-9]{64}$/.test(expectedId), "Expected archive SHA-256 required");
  const bytes = readFileSync(manifestPath);
  assert.equal(sha256(bytes), expectedId, "Archive manifest checksum mismatch");
  const manifest = JSON.parse(bytes.toString("utf8"));
  assert.equal(manifest.schema_version, 1, "Unsupported archive schema");
  assert.deepEqual(
    Object.keys(manifest.files).sort(),
    ["README.txt", "bundle.json", "data.csv", "reproduce.mjs"],
    "Unexpected archive files",
  );
  for (const [name, hash] of Object.entries(manifest.files)) {
    assert.equal(
      sha256(readFileSync(resolve(dirname(manifestPath), name))),
      hash,
      `File checksum mismatch: ${name}`,
    );
  }
  return manifest;
}

export function verifyBundle(bundle) {
  assert.equal(bundle.schema_version, 1, "Unsupported bundle schema");
  assert.equal(bundle.methodology_version, "published-classic-v1", "Unsupported methodology");
  const { dataset, parameters: p, calculations, observations } = bundle;
  assert(
    dataset.version && /^\d{4}-\d{2}-\d{2}$/.test(dataset.updated),
    "Dataset identity missing",
  );
  assert.equal(bundle.scope, "baseline-catering-both-product-units");
  assert.equal(dataset.cpi_mode, "catering");
  assert.equal(p.distributions, null, "This contract has no probability distributions");
  positive(p.drachma_per_euro, "Currency conversion");
  positive(p.reference_meat_grams, "Reference grams");
  positive(p.meat_cap, "Meat cap");
  fraction(p.meat_value_share, "Meat value share");
  fraction(p.non_meat_base, "Non-meat base");
  assert.equal(p.classic_decimals, 3, "Unsupported rounding contract");
  const sources = new Set(dataset.sources.map((s) => s.id));
  assert.equal(sources.size, dataset.sources.length, "Duplicate source ID");
  const records = new Map(observations.map((o) => [o.id, o]));
  assert.equal(records.size, observations.length, "Duplicate observation ID");
  for (const o of observations) {
    assert(sources.has(o.source_id), `Missing observation source: ${o.id}`);
    assert.equal(o.source_id, o.record.source, `Source mismatch: ${o.id}`);
    assert.equal(
      o.classification,
      o.record.estimate ? "MODELLED" : "REPORTED",
      `Observation classification: ${o.id}`,
    );
    positive(o.record.price, `Observation price: ${o.id}`);
    let normalized = o.record.price;
    for (const step of o.normalization.steps) {
      positive(step.factor, `Normalization factor: ${o.id}`);
      if (step.applies)
        normalized =
          step.operation === "multiply" ? normalized * step.factor : normalized / step.factor;
    }
    close(normalized, o.normalization.normalizedEur, `Normalization: ${o.id}`);
  }
  assert.equal(calculations.length, dataset.rows.length, "Missing annual calculation");
  assert.equal(
    dataset.rows.length,
    dataset.coverage.to - dataset.coverage.from + 1,
    "Coverage gap",
  );
  const byYear = new Map(calculations.map((c) => [c.year, c]));
  assert.equal(byYear.size, calculations.length, "Duplicate calculation year");
  const results = [];
  for (const [i, row] of dataset.rows.entries()) {
    assert.equal(row.year, dataset.coverage.from + i, "Year gap, order or duplicate");
    const c = byYear.get(row.year);
    assert(c, `Missing calculation: ${row.year}`);
    // Optional source calendars extend the old contract without altering old archives.
    // Independently derive the anchor before validating the published series below.
    if (c.price.source_calendar) {
      const calendar = c.price.source_calendar;
      const day = 86400000;
      let next = Date.UTC(row.year, 0, 1),
        days = 0,
        total = 0;
      for (const period of calendar.periods) {
        const start = Date.parse(period.start),
          end = Date.parse(period.end);
        assert.equal(start, next, "Source calendar gap or overlap");
        assert(Number.isFinite(end) && end >= start, "Invalid source calendar date");
        const length = (end - start) / day + 1;
        assert.equal(length, period.days, "Source calendar day count");
        positive(period.price, "Source calendar price");
        assert(
          period.sourceId && period.sourceUrl && period.findingKind === "regulated-gyro-pita",
          "Source calendar product or provenance",
        );
        days += length;
        total += length * period.price;
        next = end + day;
      }
      assert.equal(next, Date.UTC(row.year + 1, 0, 1), "Incomplete source calendar");
      assert.equal(days, calendar.days, "Source calendar total days");
      close(total / days, calendar.weightedMean, "Source calendar mean");
      positive(calendar.regulationUplift, "Regulation uplift");
      close(
        ((total / days) * calendar.regulationUplift) / p.drachma_per_euro,
        c.price.configured_anchor.price,
        "Source calendar anchor",
      );
    }
    assert(
      c.source_ids.length > 0 && c.source_ids.every((id) => sources.has(id)),
      `Missing source: ${row.year}`,
    );
    assert(
      c.source_ids.includes(row.wage_source) && c.source_ids.includes(row.price_source),
      `Row source mismatch: ${row.year}`,
    );
    assert(
      c.observation_ids.every((id) => records.get(id)?.record.year === row.year),
      `Observation link: ${row.year}`,
    );
    const classification =
      c.price.derivation.kind === "interpolation"
        ? "INTERPOLATED"
        : c.price.derivation.kind === "anchor" && !row.price_estimate
          ? "DERIVED"
          : "MODELLED";
    assert.equal(c.price.classification, classification, `Price classification: ${row.year}`);
    assert.equal(c.product.classification, "MODELLED", `Product classification: ${row.year}`);
    const w = c.wage;
    assert.equal(w.income_tax_deducted, false, "Income tax outside this calculation contract");
    assert.equal(
      w.annual_payroll_verified,
      false,
      "Convention must not claim a verified payroll calendar",
    );
    assert(["EUR", "GRD"].includes(w.currency), "Unknown currency");
    positive(w.original_gross, "Gross wage");
    fraction(w.contribution_fraction, "Invalid employee contribution rate");
    assert(
      Number.isInteger(w.payments) && w.payments > 0 && w.payments <= 14,
      "Payment convention",
    );
    const gross = w.original_gross / (w.currency === "GRD" ? p.drachma_per_euro : 1);
    const payment = gross * (1 - w.contribution_fraction);
    const income = round(payment * (w.payments / 12), 2);
    close(round(gross, 2), row.gross_eur, `${row.year}: gross conversion`);
    close(round(payment, 2), row.salary_contract_eur, `${row.year}: one payment`);
    close(income, row.salary_annualized_eur, `${row.year}: annualised income`);
    close(
      round(w.contribution_fraction * 100, 2),
      row.contribution_rate_pct,
      `${row.year}: contributions`,
    );
    close(w.payments, row.salary_payments, `${row.year}: payments`);
    positive(c.product.meat_grams, "Meat grams");
    assert(c.product.fries_share >= 0 && c.product.fries_share <= 1, "Fries share");
    positive(row.price_era_eur, "Period price");
    const raw =
      p.meat_value_share * Math.min(c.product.meat_grams / p.reference_meat_grams, p.meat_cap) +
      (1 - p.meat_value_share) * (p.non_meat_base + (1 - p.non_meat_base) * c.product.fries_share);
    const equivalence = round(raw, p.classic_decimals);
    const factor = round(1 / equivalence, p.classic_decimals);
    const price = round(row.price_era_eur * factor, p.classic_decimals);
    const indexEra = Math.round(income / row.price_era_eur);
    const indexClassic = Math.round(income / price);
    for (const [field, value] of Object.entries({
      classic_equivalence: equivalence,
      classic_factor: factor,
      price_classic_eur: price,
      index_era: indexEra,
      index_classic: indexClassic,
    }))
      close(value, row[field], `${row.year}: ${field}`);
    for (const [field, value] of Object.entries({
      equivalenceRaw: raw,
      equivalence,
      factor,
      price,
      quotient: income / price,
      index: indexClassic,
    }))
      close(value, c.published_classic_steps[field], `${row.year}: step ${field}`);
    positive(row.price_era_min_eur, "Price lower bound");
    assert(
      row.price_era_min_eur <= row.price_era_eur && row.price_era_eur <= row.price_era_max_eur,
      "Price bounds order",
    );
    close(Math.round(income / row.price_era_max_eur), row.index_min, `${row.year}: index minimum`);
    close(Math.round(income / row.price_era_min_eur), row.index_max, `${row.year}: index maximum`);
    results.push({ year: row.year, income, index_era: indexEra, index_classic: indexClassic });
  }
  return {
    version: dataset.version,
    years: results.length,
    results,
    scope: bundle.verification_scope,
  };
}

/** CSV is checked independently against the archived JSON, including all legacy columns. */
export function csvFromBundle(bundle) {
  const cell = (v) => {
    const s = v === null || v === undefined ? "" : String(v);
    return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
  };
  const fields = Object.keys(bundle.dataset.rows[0]);
  return [
    fields.join(","),
    ...bundle.dataset.rows.map((r) => fields.map((f) => cell(r[f])).join(",")),
  ].join("\n");
}

if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
  try {
    const file = process.argv[2];
    assert(file, "Usage: node reproduce.mjs bundle.json [data.csv] [manifest.json archive-SHA256]");
    if (process.argv[4] || process.argv[5]) verifyDownload(process.argv[4], process.argv[5]);
    const bundle = JSON.parse(readFileSync(file, "utf8"));
    const result = verifyBundle(bundle);
    if (process.argv[3])
      assert.equal(
        readFileSync(process.argv[3], "utf8"),
        csvFromBundle(bundle),
        "CSV differs from JSON",
      );
    process.stdout.write(JSON.stringify(result, null, 2) + "\n");
  } catch (error) {
    process.stderr.write(`Reproduction failed: ${error.message}\n`);
    process.exitCode = 1;
  }
}
