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

import {
  DEFAULT_CONTAINER_NAME,
  DEFAULT_CONTAINER_API_URL,
  DEFAULT_CONTAINER_BROKER_URL,
  DEFAULT_HEARTBEAT_INTERVAL_SECONDS,
  buildGatewayConfig,
  parseArgs,
  resolveComposeNetworkName,
  resolveComposeProjectName,
  resolveConfigDirectory,
  shouldClaimGateway,
} from "./test-gateway.mjs";

test("resolveComposeProjectName prefers COMPOSE_PROJECT_NAME", () => {
  assert.equal(
    resolveComposeProjectName("C:/Users/test/backend-php", { COMPOSE_PROJECT_NAME: "custom-stack" }),
    "custom-stack"
  );
});

test("resolveComposeProjectName falls back to backend directory name", () => {
  assert.equal(resolveComposeProjectName("C:/Users/test/backend-php", {}), "backend-php");
});

test("resolveComposeNetworkName derives the default compose network", () => {
  assert.equal(
    resolveComposeNetworkName("C:/Users/test/backend-php", { COMPOSE_PROJECT_NAME: "custom-stack" }),
    "custom-stack_default"
  );
});

test("resolveConfigDirectory uses the default temp folder when none is provided", () => {
  assert.equal(
    resolveConfigDirectory("C:/Users/test/backend-php"),
    "C:\\Users\\test\\backend-php\\.tmp\\test-gateway"
  );
});

test("shouldClaimGateway requires a token when no credentials are present", () => {
  assert.equal(shouldClaimGateway({}, ""), true);
  assert.equal(shouldClaimGateway({ gatewayId: 12, agentToken: "secret" }, ""), false);
  assert.equal(shouldClaimGateway({ gatewayId: 12, agentToken: "secret" }, "fresh-token"), true);
});

test("parseArgs accepts help without an explicit action", () => {
  const options = parseArgs(["--help"]);

  assert.equal(options.action, "start");
  assert.equal(options.help, true);
});

test("parseArgs accepts copy config mode", () => {
  const options = parseArgs(["start", "--copy-config"]);

  assert.equal(options.copyConfig, true);
});

test("buildGatewayConfig applies defaults for a dockerized gateway", () => {
  const config = buildGatewayConfig({});

  assert.equal(config.apiUrl, DEFAULT_CONTAINER_API_URL);
  assert.equal(config.brokerUrl, DEFAULT_CONTAINER_BROKER_URL);
  assert.equal(config.hostname, DEFAULT_CONTAINER_NAME);
  assert.equal(config.restartMode, "spawn");
  assert.equal(config.heartbeatIntervalSeconds, DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
  assert.equal(config.installDir, "/opt/truckwash-edge-agent");
  assert.equal(config.agentPath, "/opt/truckwash-edge-agent/agent.php");
  assert.equal(config.serviceUnitPath, "/opt/truckwash-edge-agent/truckwash-edge-agent.service");
  assert.equal(config.serviceName, DEFAULT_CONTAINER_NAME);
});

test("buildGatewayConfig folds claim response into the generated config", () => {
  const config = buildGatewayConfig({
    existingConfig: {
      installedVersion: "0.9.0",
      targetVersion: "0.9.1",
    },
    hostname: "gw-docker-1",
    claim: {
      gateway: { id: 44 },
      agent_token: "agent-secret",
      release_channel: "stable",
    },
    heartbeatIntervalSeconds: 7,
  });

  assert.equal(config.gatewayId, 44);
  assert.equal(config.agentToken, "agent-secret");
  assert.equal(config.releaseChannel, "stable");
  assert.equal(config.hostname, "gw-docker-1");
  assert.equal(config.installedVersion, "0.9.0");
  assert.equal(config.targetVersion, "0.9.1");
  assert.equal(config.heartbeatIntervalSeconds, 7);
});
