#!/usr/bin/env node
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

import {
  findForbiddenActiveDeprecatedEndpoints,
  REPORT_LIFECYCLE_VERSION
} from "../internal/dev-report-lifecycle.mjs";
import { tempReportPath } from "./src/report-paths.mjs";

const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const reportPath = tempReportPath("dev-m2-deploy-smoke-active.json");

function assertString(value, label) {
  assert.equal(typeof value, "string", `${label} must be a string`);
  assert.ok(value.trim().length > 0, `${label} must not be empty`);
}

function assertObject(value, label) {
  assert.ok(value && typeof value === "object" && !Array.isArray(value), `${label} must be an object`);
}

function assertArray(value, label) {
  assert.ok(Array.isArray(value), `${label} must be an array`);
}

function assertTimestamp(value, label) {
  assertString(value, label);
  assert.ok(!Number.isNaN(Date.parse(value)), `${label} must be a timestamp`);
}

async function main() {
  const raw = await readFile(reportPath, "utf8");
  const report = JSON.parse(raw);
  assertObject(report, "report");
  assert.equal(report.issue, "pikasTech/HWLAB#23");
  assert.equal(report.taskId, "m2-dev-deploy-smoke");
  assertObject(report.reportLifecycle, "reportLifecycle");
  assert.equal(report.reportLifecycle.version, REPORT_LIFECYCLE_VERSION);
  assert.equal(report.reportLifecycle.state, "active");
  assert.equal(report.reportLifecycle.activeEndpoint, "http://74.48.78.17:16667");
  assert.equal(report.reportLifecycle.activeBrowserEndpoint, "http://74.48.78.17:16666");
  assert.equal(report.reportLifecycle.deprecatedEndpoint, null);
  assert.deepEqual(
    findForbiddenActiveDeprecatedEndpoints(report, raw),
    [],
    "active M2 report must not present deprecated public ports as current endpoint evidence"
  );
  assert.equal(report.status, "pass");
  assert.equal(report.endpoint, "http://74.48.78.17:16667");
  assert.equal(report.frontendEndpoint, "http://74.48.78.17:16666");
  assertTimestamp(report.generatedAt, "generatedAt");

  assertObject(report.runtimeSmoke, "runtimeSmoke");
  assert.equal(report.runtimeSmoke.mode, "live-read-only");
  assert.equal(report.runtimeSmoke.status, "pass");
  assert.equal(report.runtimeSmoke.apiEndpoint, "http://74.48.78.17:16667");
  assert.equal(report.runtimeSmoke.frontendEndpoint, "http://74.48.78.17:16666");
  assertObject(report.runtimeSmoke.safety, "runtimeSmoke.safety");
  for (const field of ["prodTouched", "secretsRead", "restarts", "deployAttempted", "heavyE2E", "unideskRuntimeSubstitute"]) {
    assert.equal(report.runtimeSmoke.safety[field], false, `runtimeSmoke.safety.${field}`);
  }
  assertObject(report.runtimeSmoke.legacyPublicEndpoint, "runtimeSmoke.legacyPublicEndpoint");
  assert.equal(report.runtimeSmoke.legacyPublicEndpoint.endpoint, "http://74.48.78.17:6667");
  assert.equal(report.runtimeSmoke.legacyPublicEndpoint.activeGreenEligible, false);

  assertObject(report.devPreconditions, "devPreconditions");
  assert.equal(report.devPreconditions.status, "pass");
  assertArray(report.devPreconditions.commands, "devPreconditions.commands");
  assertArray(report.devPreconditions.evidence, "devPreconditions.evidence");
  assert.ok(
    report.devPreconditions.commands.includes("curl -fsS --max-time 10 http://74.48.78.17:16667/health"),
    "devPreconditions.commands missing /health probe"
  );
  assert.ok(
    report.devPreconditions.commands.includes("curl -fsS --max-time 10 http://74.48.78.17:16667/health/live"),
    "devPreconditions.commands missing /health/live probe"
  );
  assert.ok(
    report.devPreconditions.commands.includes("curl -fsS --max-time 10 http://74.48.78.17:16666/"),
    "devPreconditions.commands missing frontend probe"
  );

  console.log("validated m2-dev-deploy-smoke-active.json");
}

try {
  await main();
} catch (error) {
  process.stderr.write(`[validate-m2-dev-deploy-smoke-active] ${error instanceof Error ? error.message : String(error)}\n`);
  process.exitCode = 1;
}
