import assert from "node:assert/strict";
import test from "node:test";

import { readStructuredFile } from "./src/structured-config.mjs";

const CLOUD_API_ARCHITECTURE_ENV = Object.freeze({
  HWLAB_KAFKA_DIRECT_PUBLISH_ENABLED: "true",
  HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED: "true",
  HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED: "true",
  HWLAB_KAFKA_TRANSACTIONAL_PROJECTOR_ENABLED: "false",
  HWLAB_KAFKA_PROJECTION_OUTBOX_RELAY_ENABLED: "false",
  HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED: "false"
});

const CLOUD_WEB_ARCHITECTURE_ENV = Object.freeze({
  HWLAB_WORKBENCH_LIVE_KAFKA_SSE_ENABLED: "true",
  HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED: "true",
  HWLAB_WORKBENCH_PROJECTION_REALTIME_ENABLED: "false"
});

const LIVE_KAFKA_CONTRACT = Object.freeze({
  HWLAB_KAFKA_STDIO_TOPIC: "codex-stdio.raw.v1",
  HWLAB_KAFKA_AGENTRUN_EVENT_TOPIC: "agentrun.event.v1",
  HWLAB_KAFKA_EVENT_TOPIC: "hwlab.event.v1",
  HWLAB_KAFKA_AGENTRUN_EVENT_GROUP_ID: "hwlab-v03-agentrun-event-direct-publish",
  HWLAB_KAFKA_PROJECTOR_GROUP_ID: "hwlab-v03-agentrun-event-projector",
  HWLAB_KAFKA_HWLAB_EVENT_GROUP_ID: "hwlab-v03-workbench-live-sse"
});

const UNIDESK_OWNED_REFRESH_REPLAY_ENV = Object.freeze([
  "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_GROUP_PREFIX",
  "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_TIMEOUT_MS",
  "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_SCAN_LIMIT",
  "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_MATCHED_EVENT_LIMIT",
  "HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_LIVE_BUFFER_LIMIT"
]);

const ISOLATED_DEBUG_CONTRACT = Object.freeze({
  HWLAB_WORKBENCH_KAFKA_DEBUG_REPLAY_ENABLED: "true",
  HWLAB_KAFKA_HWLAB_DEBUG_EVENT_TOPIC: "hwlab.event.debug.v1",
  HWLAB_KAFKA_HWLAB_DEBUG_GROUP_PREFIX: "hwlab-v03-workbench-isolated-debug",
  HWLAB_WORKBENCH_KAFKA_DEBUG_REPLAY_LIMIT: "200",
  HWLAB_WORKBENCH_KAFKA_DEBUG_REPLAY_TIMEOUT_MS: "15000"
});

test("v03 YAML fixes direct publish, live Kafka SSE and retention replay authority", async () => {
  const deploy = await readStructuredFile(process.cwd(), "deploy/deploy.yaml");
  const cloudApi = deploy.lanes?.v03?.services?.find((service) => service.serviceId === "hwlab-cloud-api");
  const cloudWeb = deploy.lanes?.v03?.serviceDeclarations?.["hwlab-cloud-web"];
  assert.ok(cloudApi, "deploy.lanes.v03.services must declare hwlab-cloud-api");
  assert.ok(cloudWeb, "deploy.lanes.v03.serviceDeclarations must declare hwlab-cloud-web");

  for (const [name, expected] of Object.entries(CLOUD_API_ARCHITECTURE_ENV)) assert.equal(cloudApi.env?.[name], expected, `${name} must fix the cloud-api Kafka authority`);
  for (const [name, expected] of Object.entries(CLOUD_WEB_ARCHITECTURE_ENV)) assert.equal(cloudWeb.env?.[name], expected, `${name} must fix the cloud-web Kafka authority`);
  for (const [name, expected] of Object.entries(LIVE_KAFKA_CONTRACT)) {
    assert.equal(cloudApi.env?.[name], expected, `${name} must preserve the pure Kafka live contract`);
  }
  for (const name of UNIDESK_OWNED_REFRESH_REPLAY_ENV) {
    assert.equal(cloudApi.env?.[name], undefined, `${name} is injected from UniDesk config/hwlab-node-lanes.yaml and must not have a second value in HWLAB deploy.yaml`);
  }
  for (const [name, expected] of Object.entries(ISOLATED_DEBUG_CONTRACT)) {
    assert.equal(cloudApi.env?.[name], expected, `${name} must be independently YAML-owned for debug only`);
  }

  assert.equal(cloudApi.env?.HWLAB_SESSION_COOKIE_DOMAIN, undefined, "internal and public origins require host-scoped session cookies");
  assert.notEqual(cloudApi.env?.HWLAB_KAFKA_AGENTRUN_EVENT_GROUP_ID, "hwlab-v03-agentrun-event-bridge");
  assert.equal(cloudWeb.env?.HWLAB_WORKBENCH_KAFKA_DEBUG_REPLAY_ENABLED, "true");
});
