import { expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { updateApp } from "../src/update.ts";

test("updateApp migrates project files without marking an unverified update complete", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-update-"));
  mkdirSync(join(root, "app", "pages"), { recursive: true });
  writeFileSync(
    join(root, "package.json"),
    JSON.stringify({
      name: "legacy-app",
      scripts: { dev: "wrnexus dev ." },
      dependencies: { "@wrnexus/core": "0.2.13" },
      wrnexus: { version: "0.2.13" },
    }),
  );
  writeFileSync(join(root, ".gitignore"), "node_modules/\n");

  try {
    const result = updateApp(root, "0.2.14", false);
    expect(result).not.toBeNull();
    const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
    expect(pkg.dependencies["@wrnexus/core"]).toBe("^0.2.14");
    expect(pkg.scripts.build).toBe("wrnexus build .");
    expect(pkg.scripts.start).toBe("bun dist/server.js");
    expect(pkg.scripts.production).toBe("bun run build && bun run start");
    expect(pkg.wrnexus.version).toBe("0.2.13");

    const gitignore = readFileSync(join(root, ".gitignore"), "utf8");
    expect(gitignore).toContain("!.env.example");
    expect(gitignore).toContain("mobile/android/");
    expect(readFileSync(join(root, "public", "llms.txt"), "utf8")).toContain("# WrNexus");
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test("0.2.18 migration adds helpers to existing apps and is idempotent", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-update-"));
  mkdirSync(join(root, "app", "pages"), { recursive: true });
  writeFileSync(
    join(root, "package.json"),
    JSON.stringify({
      name: "existing-app",
      dependencies: { "@wrnexus/core": "^0.2.17" },
      wrnexus: { version: "0.2.17" },
    }),
  );

  try {
    updateApp(root, "0.2.18", false);
    updateApp(root, "0.2.18", false);
    const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
    expect(pkg.dependencies["@wrnexus/helpers"]).toBe("^0.2.18");
    expect(
      Object.keys(pkg.dependencies).filter((name) => name === "@wrnexus/helpers"),
    ).toHaveLength(1);
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test("0.2.19 migration adds VS Code formatting defaults without overwriting settings", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-update-"));
  mkdirSync(join(root, "app", "pages"), { recursive: true });
  mkdirSync(join(root, ".vscode"), { recursive: true });
  writeFileSync(
    join(root, "package.json"),
    JSON.stringify({ name: "editor-app", wrnexus: { version: "0.2.18" } }),
  );
  writeFileSync(join(root, ".gitignore"), ".vscode/\n");
  writeFileSync(join(root, ".prettierignore"), "node_modules/\n");
  writeFileSync(join(root, ".vscode", "settings.json"), '{"editor.tabSize":4}\n');

  try {
    updateApp(root, "0.2.19", false);
    updateApp(root, "0.2.19", false);

    expect(readFileSync(join(root, ".vscode", "settings.json"), "utf8")).toBe(
      '{"editor.tabSize":4}\n',
    );
    expect(readFileSync(join(root, ".vscode", "extensions.json"), "utf8")).toContain(
      "wrnexus.wrnexus",
    );
    expect(readFileSync(join(root, ".vscode", "extensions.json"), "utf8")).toBe(
      '{\n  "recommendations": ["wrnexus.wrnexus", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint"]\n}\n',
    );
    expect(readFileSync(join(root, ".prettierignore"), "utf8")).toBe("node_modules/\nCLAUDE.md\n");
    const gitignore = readFileSync(join(root, ".gitignore"), "utf8");
    expect(gitignore).not.toContain(".vscode/\n");
    expect(gitignore).toContain("!.vscode/settings.json");
    expect(gitignore).toContain("!.vscode/extensions.json");
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});

test("0.2.20 migration repairs only the legacy generated recommendations layout", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-update-"));
  mkdirSync(join(root, "app", "pages"), { recursive: true });
  mkdirSync(join(root, ".vscode"), { recursive: true });
  writeFileSync(
    join(root, "package.json"),
    JSON.stringify({ name: "editor-app", wrnexus: { version: "0.2.19" } }),
  );
  writeFileSync(
    join(root, ".vscode", "extensions.json"),
    JSON.stringify(
      {
        recommendations: ["wrnexus.wrnexus", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint"],
      },
      null,
      2,
    ) + "\n",
  );

  try {
    updateApp(root, "0.2.20", false);
    expect(readFileSync(join(root, ".vscode", "extensions.json"), "utf8")).toBe(
      '{\n  "recommendations": ["wrnexus.wrnexus", "esbenp.prettier-vscode", "dbaeumer.vscode-eslint"]\n}\n',
    );
  } finally {
    rmSync(root, { recursive: true, force: true });
  }
});
