import assert from "node:assert/strict";
import test from "node:test";
import { buildRowKey, deriveKpiStatus, parseDueDateIso, parseNumber, parsePercent } from "@/lib/kpis/parsing";

test("parsePercent extracts mixed percent strings", () => {
  assert.equal(parsePercent("90% PROMEDIO GENERAL"), 90);
  assert.equal(parsePercent("PLAN 50%"), 50);
  assert.equal(parsePercent("SIN PORCENTAJE"), null);
});

test("parseNumber supports thousands and decimals", () => {
  assert.equal(parseNumber("800,000"), 800000);
  assert.equal(parseNumber("280,000"), 280000);
  assert.equal(parseNumber("22.5"), 22.5);
});

test("parseDueDateIso handles relative and month text", () => {
  assert.equal(parseDueDateIso("EN UNA SEMANA", "2026-03-16"), "2026-03-23");
  assert.equal(parseDueDateIso("PLAN CON TIEMPOS PARA ABRIL", "2026-03-16"), "2026-04-15");
  assert.equal(parseDueDateIso("MAYO 100%", "2026-03-16"), "2026-05-15");
});

test("deriveKpiStatus classifies by compliance and due date", () => {
  assert.equal(
    deriveKpiStatus({
      compliancePct: 95,
      quantityValue: null,
      dueDateIso: null,
      todayIso: "2026-03-16",
    }),
    "on_track"
  );

  assert.equal(
    deriveKpiStatus({
      compliancePct: 65,
      quantityValue: null,
      dueDateIso: null,
      todayIso: "2026-03-16",
    }),
    "watch"
  );

  assert.equal(
    deriveKpiStatus({
      compliancePct: null,
      quantityValue: null,
      dueDateIso: "2026-03-10",
      todayIso: "2026-03-16",
    }),
    "risk"
  );
});

test("buildRowKey is stable for same content", () => {
  const keyA = buildRowKey(["Métrica semanal", "PLAN 50%", "", undefined]);
  const keyB = buildRowKey(["Métrica semanal", "PLAN 50%", "", undefined]);

  assert.equal(keyA, keyB);
});
