#!/usr/bin/env node

// PR 1 of the resilience repair plan (§3.3). Writes the per-country
// low-carbon share of electricity generation. Read by scoreEnergy v2 via
// `resilience:low-carbon-generation:v1`.
//
// Source: Our World in Data Grapher, "Share of electricity generated by
// low-carbon sources", sourced from Ember and Energy Institute. This is the
// same power-system-security concept the scorer expects: low-carbon sources
// are renewables (including hydropower) plus nuclear, measured as a percentage
// of total electricity generation.
//
// The original implementation summed three World Bank WDI components
// (EG.ELC.NUCL.ZS + EG.ELC.RNEW.ZS + EG.ELC.HYRO.ZS) at each country's latest
// common component year. That calculation was correct for WDI, but the latest
// common component year froze at 2021 because the upstream component series
// lagged. OWID/Ember publishes newer annual electricity-mix data, so the Redis
// key and scorer contract stay stable while the source and content-age budget
// move to the fresher cadence.

import { loadEnvFile, CHROME_UA, runSeed, withRetry } from './_seed-utils.mjs';
import { resolveIso2 } from './_country-resolver.mjs';
// Shared content-age helper for per-country annual payloads.
import { wbCountryDictContentMeta } from './_wb-country-dict-content-age-helpers.mjs';

// OWID/Ember is annual and typically publishes the prior-year electricity mix.
// 18 months clears the normal annual lag plus release drift, while reverting
// the interim 60-month WDI stall tolerance from issue #4219.
export const LOW_CARBON_MAX_CONTENT_AGE_MIN = 18 * 30 * 24 * 60;

loadEnvFile(import.meta.url);

const OWID_LOW_CARBON_CSV_URL =
  'https://ourworldindata.org/grapher/share-electricity-low-carbon.csv?v=1&csvType=full&useColumnShortNames=true';
const OWID_VALUE_COLUMN = 'low_carbon_share_of_electricity__pct';
const CANONICAL_KEY = 'resilience:low-carbon-generation:v1';
const CACHE_TTL = 35 * 24 * 3600;
// Coverage floor — a sanity guard against a truncated/partial CSV fetch, NOT a
// tight coverage assertion. Live OWID `share-electricity-low-carbon` maps to
// ~209 ISO2 countries (verified 2026-06-10), but it is NOT a strict superset of
// the retired WB feed: 5 micro-states present in WB (AD, FM, MH, PW, TV) are
// absent from OWID. A 200 floor leaves only a ~9-country margin, so ordinary
// year-to-year OWID churn or a few ISO-resolution misses could false-trip
// validation → no publish → false STALE_SEED. 180 still catches a real
// collapse (a truncated fetch yields far fewer rows) while absorbing normal
// drift; the newest-year check below is the primary freshness guard.
const MIN_COUNTRIES = 180;
const MAX_NEWEST_YEAR_LAG = 2;

export function getLowCarbonMinNewestYear(nowMs = Date.now()) {
  return new Date(nowMs).getUTCFullYear() - MAX_NEWEST_YEAR_LAG;
}

function parseDelimitedRow(line, delimiter = ',') {
  const cells = [];
  let current = '';
  let inQuotes = false;

  for (let idx = 0; idx < line.length; idx += 1) {
    const char = line[idx];
    const next = line[idx + 1];

    if (char === '"') {
      if (inQuotes && next === '"') {
        current += '"';
        idx += 1;
      } else {
        inQuotes = !inQuotes;
      }
      continue;
    }

    if (char === delimiter && !inQuotes) {
      cells.push(current);
      current = '';
      continue;
    }

    current += char;
  }

  cells.push(current);
  return cells.map((cell) => cell.trim());
}

function parseDelimitedText(text, delimiter = ',') {
  const lines = String(text || '')
    .replace(/^\uFEFF/, '')
    .split(/\r\n|\r|\n/)
    .map((line) => line.trim())
    .filter(Boolean);
  if (lines.length < 2) return [];

  const headers = parseDelimitedRow(lines[0], delimiter);
  return lines.slice(1).map((line) => {
    const values = parseDelimitedRow(line, delimiter);
    return Object.fromEntries(headers.map((header, idx) => [header, values[idx] ?? '']));
  });
}

export function buildLowCarbonCountriesFromOwidRows(rows) {
  const latestByIso = new Map();

  for (const row of rows) {
    const iso3 = String(row?.code || '').trim().toUpperCase();
    if (!/^[A-Z]{3}$/.test(iso3)) continue;

    const iso2 = resolveIso2({ iso3, name: row?.entity });
    if (!iso2) continue;

    const year = Number(row?.year);
    if (!Number.isInteger(year) || year < 1900) continue;

    const valueCell = row?.[OWID_VALUE_COLUMN];
    if (valueCell == null || String(valueCell).trim() === '') continue;

    const value = Number(valueCell);
    if (!Number.isFinite(value)) continue;

    const prev = latestByIso.get(iso2);
    if (prev && prev.year >= year) continue;

    latestByIso.set(iso2, {
      value: Math.max(0, Math.min(value, 100)),
      year,
      source: 'OWID share-electricity-low-carbon',
      sourceEntity: row?.entity || iso2,
      sourceCode: iso3,
    });
  }

  return Object.fromEntries([...latestByIso.entries()].sort(([a], [b]) => a.localeCompare(b)));
}

export function parseOwidLowCarbonCsv(csvText) {
  const rows = parseDelimitedText(csvText, ',');
  if (rows.length === 0) throw new Error('OWID low-carbon CSV: no data rows');

  const headers = Object.keys(rows[0] || {});
  for (const required of ['entity', 'code', 'year', OWID_VALUE_COLUMN]) {
    if (!headers.includes(required)) {
      throw new Error(`OWID low-carbon CSV schema changed: missing ${required}`);
    }
  }

  return buildLowCarbonCountriesFromOwidRows(rows);
}

async function fetchOwidLowCarbonCsv() {
  return withRetry(
    async () => {
      const resp = await fetch(OWID_LOW_CARBON_CSV_URL, {
        headers: {
          'User-Agent': CHROME_UA,
          Accept: 'text/csv,text/plain,*/*',
        },
        signal: AbortSignal.timeout(30_000),
      });
      if (!resp.ok) throw new Error(`OWID low-carbon electricity: HTTP ${resp.status}`);
      return resp.text();
    },
    2,
    750,
  );
}

async function fetchLowCarbonGeneration() {
  const countries = parseOwidLowCarbonCsv(await fetchOwidLowCarbonCsv());
  return { countries, seededAt: new Date().toISOString() };
}

export function validateLowCarbonGeneration(data, nowMs = Date.now()) {
  if (!data?.countries || typeof data.countries !== 'object') return false;

  const entries = Object.values(data.countries);
  const years = entries
    .map((entry) => entry?.year)
    .filter((year) => Number.isInteger(year));

  if (entries.length < MIN_COUNTRIES) return false;
  if (years.length === 0 || Math.max(...years) < getLowCarbonMinNewestYear(nowMs)) return false;
  return true;
}

export function declareRecords(data) {
  return Object.keys(data?.countries || {}).length;
}

if (process.argv[1]?.endsWith('seed-low-carbon-generation.mjs')) {
  runSeed('resilience', 'low-carbon-generation', CANONICAL_KEY, fetchLowCarbonGeneration, {
    validateFn: validateLowCarbonGeneration,
    ttlSeconds: CACHE_TTL,
    sourceVersion: `owid-low-carbon-electricity-${new Date().getFullYear()}`,
    recordCount: (data) => Object.keys(data?.countries ?? {}).length,
    declareRecords,
    schemaVersion: 1,
    maxStaleMin: 8 * 24 * 60, // weekly cadence + 1 day slack

    // Content-age contract: OWID/Ember is annual, and contentMeta takes the
    // max year across countries so late reporters do not stale the whole seed.
    contentMeta: wbCountryDictContentMeta,
    maxContentAgeMin: LOW_CARBON_MAX_CONTENT_AGE_MIN,
  }).catch((err) => {
    const _cause = err.cause ? ` (cause: ${err.cause.message || err.cause.code || err.cause})` : '';
    console.error('FATAL:', (err.message || err) + _cause);
    process.exit(1);
  });
}
