import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { collectMeasureVersions } from "./versions";

describe("collectMeasureVersions", () => {
  function writeJson(filePath: string, value: unknown) {
    fs.mkdirSync(path.dirname(filePath), { recursive: true });
    fs.writeFileSync(filePath, JSON.stringify(value, null, 2));
  }

  function writeFile(filePath: string, content: string) {
    fs.mkdirSync(path.dirname(filePath), { recursive: true });
    fs.writeFileSync(filePath, content);
  }

  let tmpDir: string;

  beforeEach(() => {
    tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "measure-versions-"));
  });

  afterEach(() => {
    fs.rmSync(tmpDir, { recursive: true, force: true });
  });

  it("returns ok when every discovered package aligns", async () => {
    writeJson(path.join(tmpDir, "package.json"), {
      name: "consumer",
      devDependencies: { "@tailor-platform/erp-kit": "0.29.0" },
    });
    writeJson(path.join(tmpDir, "apps/web/backend/package.json"), {
      name: "consumer-backend",
      dependencies: {
        "@tailor-platform/erp-kit": "0.29.0",
        "@tailor-platform/sdk": "1.55.0",
      },
    });
    writeJson(path.join(tmpDir, "apps/web/frontend/package.json"), {
      name: "consumer-frontend",
      dependencies: { "@tailor-platform/app-shell": "1.3.0" },
    });

    const result = await collectMeasureVersions({
      appsRoot: "apps",
      modulesRoot: "modules",
      cwd: tmpDir,
      installedVersion: "0.29.0",
    });
    expect(result.status).toBe("ok");
    expect(result.findings).toEqual([]);
  });

  it("counts errors/warnings as violations but not info", async () => {
    writeJson(path.join(tmpDir, "package.json"), { name: "consumer" });
    writeJson(path.join(tmpDir, "apps/web/backend/package.json"), {
      name: "consumer-backend",
      dependencies: { "@tailor-platform/sdk": "1.20.0" },
    });
    writeFile(
      path.join(tmpDir, ".agents/skills/erp-kit-app-1-requirements/SKILL.md"),
      `---\nmetadata:\n  erp-kit-version: "0.0.0"\n---\n\nbody\n`,
    );

    const result = await collectMeasureVersions({
      appsRoot: "apps",
      modulesRoot: "modules",
      cwd: tmpDir,
      installedVersion: "0.29.0",
    });
    expect(result.status).toBe("violations");
    expect(result.violationCount).toBe(1);
    expect(result.findings).toHaveLength(2);
  });

  it("honours custom appsRoot / modulesRoot overrides", async () => {
    writeJson(path.join(tmpDir, "package.json"), { name: "consumer" });
    writeJson(path.join(tmpDir, "services/web/backend/package.json"), {
      name: "service-backend",
      dependencies: { "@tailor-platform/sdk": "1.20.0" },
    });

    const withoutOverride = await collectMeasureVersions({
      appsRoot: "apps",
      modulesRoot: "modules",
      cwd: tmpDir,
      installedVersion: "0.29.0",
    });
    expect(withoutOverride.status).toBe("ok");

    const withOverride = await collectMeasureVersions({
      appsRoot: "services",
      modulesRoot: "packages/modules",
      cwd: tmpDir,
      installedVersion: "0.29.0",
    });
    expect(withOverride.status).toBe("violations");
    expect(withOverride.violationCount).toBe(1);
  });
});
