import { expect, test } from "bun:test";
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { currentCliVersion } from "../src/update-notifier.ts";
import { addWorkspaceApp, insertWorkspaceApp, workspaceFiles } from "../src/workspace.ts";

test("workspace templates pin the running framework release", () => {
  const files = workspaceFiles("acme");
  const rootPackage = JSON.parse(files["package.json"]!);
  const sharedPackage = JSON.parse(files["packages/shared/package.json"]!);
  const version = currentCliVersion();

  expect(rootPackage.devDependencies["@wrnexus/cli"]).toBe(version);
  expect(sharedPackage.dependencies["@wrnexus/pubsub"]).toBe(version);
  expect(files["README.md"]).toContain("http://127.0.0.1:3000");
  expect(files["README.md"]).toContain("internal gateway targets");
});

test("insertWorkspaceApp handles nested arrays and comments", () => {
  const source = `const config = {
  apps: [
    { name: "web", dir: "apps/web", domains: ["localhost"] },
    { name: "admin", dir: "apps/admin", domains: ["admin.localhost"], auth: { allowIps: ["::1"] } }, // ]
  ],
};
export default config;
`;
  const result = insertWorkspaceApp(source, {
    name: "reports",
    dir: "apps/reports",
    domains: ["reports.localhost"],
  });

  expect(result).toContain(
    '{ name: "reports", dir: "apps/reports", domains: ["reports.localhost"] },',
  );
  expect(result.indexOf('name: "reports"')).toBeLessThan(result.indexOf("\n  ],"));
});

test("workspace add scaffolds and registers an app", async () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-workspace-add-"));
  writeFileSync(
    join(root, "wrnexus.workspace.ts"),
    'export default { apps: [{ name: "web", dir: "apps/web", domains: ["localhost"] }] };\n',
  );

  try {
    await addWorkspaceApp(root, "reports", ["--domain=reports.localhost"]);
    const manifest = JSON.parse(
      readFileSync(join(root, "apps", "reports", "package.json"), "utf8"),
    );
    const config = readFileSync(join(root, "wrnexus.workspace.ts"), "utf8");

    expect(manifest.name).toBe("reports");
    expect(manifest.wrnexus.version).toBe(currentCliVersion());
    expect(config).toContain('name: "reports"');
    expect(config).toContain('domains: ["reports.localhost"]');
    expect(existsSync(join(root, "apps", "reports", "public", "llms.txt"))).toBe(true);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});
