import assert from "node:assert/strict";
import path from "node:path";
import test from "node:test";
import { deploymentHelp, parseDeployArgs } from "./args.mjs";

test("defaults to deploy", () => {
  assert.deepEqual(parseDeployArgs([]), {
    command: "deploy",
    reconfigure: false,
    nonInteractive: false,
    runtimeUrl: null,
    webHost: null,
    webPort: null,
    config: null
  });
});

test("accepts flags before or after deploy", () => {
  assert.deepEqual(parseDeployArgs(["--non-interactive"]), {
    command: "deploy",
    reconfigure: false,
    nonInteractive: true,
    runtimeUrl: null,
    webHost: null,
    webPort: null,
    config: null
  });
  assert.deepEqual(parseDeployArgs(["deploy", "--reconfigure"]), {
    command: "deploy",
    reconfigure: true,
    nonInteractive: false,
    runtimeUrl: null,
    webHost: null,
    webPort: null,
    config: null
  });
  assert.deepEqual(parseDeployArgs(["--reconfigure", "deploy"]), {
    command: "deploy",
    reconfigure: true,
    nonInteractive: false,
    runtimeUrl: null,
    webHost: null,
    webPort: null,
    config: null
  });
});

test("supports every lifecycle command", () => {
  for (const command of ["start", "stop", "restart", "status", "logs", "doctor", "tui", "help"]) {
    assert.equal(parseDeployArgs([command]).command, command);
  }
});

test("tui accepts optional --runtime-url", () => {
  assert.deepEqual(parseDeployArgs(["tui"]), {
    command: "tui",
    reconfigure: false,
    nonInteractive: false,
    runtimeUrl: null,
    webHost: null,
    webPort: null,
    config: null
  });
  assert.deepEqual(parseDeployArgs(["tui", "--runtime-url", "http://127.0.0.1:9000/api/v1/runs"]), {
    command: "tui",
    reconfigure: false,
    nonInteractive: false,
    runtimeUrl: "http://127.0.0.1:9000/api/v1/runs",
    webHost: null,
    webPort: null,
    config: null
  });
  assert.deepEqual(parseDeployArgs(["--runtime-url", "http://example/api/v1/runs", "tui"]), {
    command: "tui",
    reconfigure: false,
    nonInteractive: false,
    runtimeUrl: "http://example/api/v1/runs",
    webHost: null,
    webPort: null,
    config: null
  });
});

test("start accepts web host and port", () => {
  assert.deepEqual(parseDeployArgs(["start", "--web-host", "127.0.0.1", "--web-port", "3310"]), {
    command: "start",
    reconfigure: false,
    nonInteractive: false,
    runtimeUrl: null,
    webHost: "127.0.0.1",
    webPort: "3310",
    config: null
  });
  assert.deepEqual(parseDeployArgs(["start", "--config", "team.yaml"]), {
    command: "start",
    reconfigure: false,
    nonInteractive: false,
    runtimeUrl: null,
    webHost: null,
    webPort: null,
    config: path.resolve("team.yaml")
  });
  assert.throws(() => parseDeployArgs(["start", "--config", "   "]), /--config requires a value/);
  assert.throws(() => parseDeployArgs(["status", "--web-port", "3000"]), /only valid with deploy, start, restart, or doctor/);
  assert.throws(() => parseDeployArgs(["tui", "--config", "team.yaml"]), /--config is only valid with deploy, start, restart, or doctor/);
  const doctor = parseDeployArgs(["doctor", "--config", "team.yaml", "--web-host", "127.0.0.2", "--web-port", "3310"]);
  assert.equal(doctor.config, path.resolve("team.yaml"));
  assert.equal(doctor.webHost, "127.0.0.2");
  assert.equal(doctor.webPort, "3310");
  assert.equal(parseDeployArgs(["--help"]).command, "help");
  assert.equal(parseDeployArgs(["restart", "-h"]).command, "help");
  assert.throws(() => parseDeployArgs(["start", "--config"]), /--config requires a value/);
  assert.throws(() => parseDeployArgs(["start", "--web-port"]), /--web-port requires a value/);
});

test("rejects unknown commands and invalid flag combinations", () => {
  assert.throws(() => parseDeployArgs(["start", "--reconfigure"]), /--reconfigure is only valid with deploy/);
  assert.throws(() => parseDeployArgs(["status", "--non-interactive"]), /--non-interactive is only valid with deploy/);
  assert.throws(
    () => parseDeployArgs(["deploy", "--reconfigure", "--non-interactive"]),
    /mutually exclusive/
  );
  assert.throws(() => parseDeployArgs(["deploy", "--runtime-url", "http://x"]), /--runtime-url is only valid with tui/);
  assert.throws(() => parseDeployArgs(["tui", "--runtime-url"]), /--runtime-url requires a value/);
  assert.throws(() => parseDeployArgs(["wat"]), /Unknown command/);
});

test("help lists lifecycle commands including tui and build readiness note", () => {
  const help = deploymentHelp();
  for (const command of ["deploy", "start", "stop", "restart", "status", "logs", "doctor", "tui", "help"]) {
    assert.match(help, new RegExp(`^${command}\\b`, "m"));
  }
  assert.match(help, /builds the TUI|build \(including TUI\)/i);
  assert.match(help, /\.\/deploy\.sh tui/);
  assert.match(help, /does not stay running/i);
});