import { describe, it } from "vitest";
import { RuleTester } from "eslint";
import path from "node:path";
import os from "node:os";
import fs from "node:fs";
import rule from "./required-cores-installed.js";

function makeFixture({ workspacePackages, manifestCores }) {
  const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), "rci-"));
  fs.writeFileSync(
    path.join(repoRoot, "pnpm-workspace.yaml"),
    `packages:\n${workspacePackages.map((p) => `  - "${p}"`).join("\n")}\n`,
  );
  const manifestDir = path.join(repoRoot, "packages", "demo", "src");
  fs.mkdirSync(manifestDir, { recursive: true });
  // We also need each declared core to exist as a package directory for the glob to resolve.
  for (const core of manifestCores) {
    fs.mkdirSync(path.join(repoRoot, "packages", `core-${core}`), { recursive: true });
  }
  const manifest = path.join(manifestDir, "feature.manifest.ts");
  fs.writeFileSync(
    manifest,
    `export const demoManifest = defineFeature({
  name: "demo",
  requiredCores: [${manifestCores.map((c) => `"${c}"`).join(", ")}],
  useCases: {},
  realtimeChannels: [],
  jobs: [],
} as const);`,
  );
  return { repoRoot, manifest };
}

const tester = new RuleTester({ languageOptions: { ecmaVersion: "latest", sourceType: "module" } });

describe("required-cores-installed", () => {
  it("passes when all declared cores are present as core-<x> packages under a workspace glob", () => {
    const { repoRoot, manifest } = makeFixture({
      workspacePackages: ["packages/*"],
      manifestCores: ["audit", "events"],
    });
    tester.run("required-cores-installed", rule, {
      valid: [
        {
          filename: manifest,
          code: fs.readFileSync(manifest, "utf8"),
          options: [{ repoRoot }],
        },
      ],
      invalid: [],
    });
  });

  it("fires for any declared core that has no matching package", () => {
    const repoRoot = fs.mkdtempSync(path.join(os.tmpdir(), "rci-"));
    fs.writeFileSync(path.join(repoRoot, "pnpm-workspace.yaml"), `packages:\n  - "packages/*"\n`);
    const manifestDir = path.join(repoRoot, "packages", "demo", "src");
    fs.mkdirSync(manifestDir, { recursive: true });
    // NB: do NOT create packages/core-realtime — that's the missing one.
    fs.mkdirSync(path.join(repoRoot, "packages", "core-audit"), { recursive: true });
    const manifest = path.join(manifestDir, "feature.manifest.ts");
    fs.writeFileSync(
      manifest,
      `export const demoManifest = defineFeature({
  name: "demo",
  requiredCores: ["audit", "realtime"],
  useCases: {},
  realtimeChannels: [],
  jobs: [],
} as const);`,
    );

    tester.run("required-cores-installed", rule, {
      valid: [],
      invalid: [
        {
          filename: manifest,
          code: fs.readFileSync(manifest, "utf8"),
          options: [{ repoRoot }],
          errors: [{ messageId: "coreNotInstalled", data: { core: "realtime" } }],
        },
      ],
    });
  });

  it("is a no-op for files that are not feature.manifest.ts", () => {
    const dir = fs.mkdtempSync(path.join(os.tmpdir(), "rci-"));
    const other = path.join(dir, "not-a-manifest.ts");
    fs.writeFileSync(other, `export const x = 1;`);
    tester.run("required-cores-installed", rule, {
      valid: [{ filename: other, code: fs.readFileSync(other, "utf8"), options: [{ repoRoot: dir }] }],
      invalid: [],
    });
  });
});
