/**
 * @file Tests for GET/POST /api/lanes/:id/features… — the HTTP surface over
 * server/lib/lane-features.js.
 * @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
 */

const os = require("node:os");
const path = require("node:path");
const fs = require("node:fs");
const http = require("node:http");

const SUITE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "ccam-features-api-"));
process.env.DASHBOARD_DB_PATH = path.join(SUITE_ROOT, "dashboard.db");
process.env.LANES_ROOT = path.join(SUITE_ROOT, "lanes");

const { describe, it, before, after } = require("node:test");
const assert = require("node:assert/strict");

const { createApp } = require("../index");
const lanesLib = require("../lib/lanes");

let server;
let PORT;

before(async () => {
  const app = createApp();
  server = http.createServer(app);
  await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
  PORT = server.address().port;
});
after(async () => {
  await new Promise((resolve) => server.close(resolve));
  fs.rmSync(SUITE_ROOT, { recursive: true, force: true });
});

function request(method, urlPath, body) {
  return new Promise((resolve, reject) => {
    const data = body ? JSON.stringify(body) : null;
    const req = http.request(
      {
        method,
        hostname: "127.0.0.1",
        port: PORT,
        path: urlPath,
        headers: {
          "Content-Type": "application/json",
          ...(data ? { "Content-Length": Buffer.byteLength(data) } : {}),
        },
      },
      (res) => {
        let raw = "";
        res.on("data", (chunk) => (raw += chunk));
        res.on("end", () => {
          let json = null;
          try {
            json = JSON.parse(raw);
          } catch {
            /* empty body ok */
          }
          resolve({ status: res.statusCode, body: json });
        });
      }
    );
    req.on("error", reject);
    if (data) req.write(data);
    req.end();
  });
}

let laneSeq = 0;
function makeLane() {
  laneSeq += 1;
  const cwd = path.join(SUITE_ROOT, `lane-cwd-${laneSeq}`);
  fs.mkdirSync(cwd, { recursive: true });
  return lanesLib.createLane({ title: `lane ${laneSeq}`, cwd, kind: "managed" });
}

describe("POST /api/lanes/:id/features/activate", () => {
  it("activates a new feature and echoes the canonicalized slug", async () => {
    const lane = makeLane();
    const res = await request("POST", `/api/lanes/${lane.id}/features/activate`, {
      slug: "feat/My Thing",
    });
    assert.equal(res.status, 200);
    assert.equal(res.body.feature.slug, "My-Thing");
    assert.equal(res.body.lane.active_feature_id, res.body.feature.id);
  });
});

describe("GET /api/lanes/:id/features", () => {
  it("lists every feature with computed pipeline_nodes", async () => {
    const lane = makeLane();
    await request("POST", `/api/lanes/${lane.id}/features/activate`, { slug: "one" });
    const res = await request("GET", `/api/lanes/${lane.id}/features`);
    assert.equal(res.status, 200);
    assert.equal(res.body.features.length, 1);
    assert.ok(Array.isArray(res.body.features[0].pipeline_nodes));
  });
});

describe("GET /api/lanes/:id/features/:slug", () => {
  it("returns one feature by slug", async () => {
    const lane = makeLane();
    await request("POST", `/api/lanes/${lane.id}/features/activate`, { slug: "one" });
    const res = await request("GET", `/api/lanes/${lane.id}/features/one`);
    assert.equal(res.status, 200);
    assert.equal(res.body.feature.slug, "one");
  });

  it("404s ENOFEATURE for an unknown slug", async () => {
    const lane = makeLane();
    const res = await request("GET", `/api/lanes/${lane.id}/features/never`);
    assert.equal(res.status, 404);
    assert.equal(res.body.error.code, "ENOFEATURE");
  });

  it("shows an archived feature's final stage after the lane moves on", async () => {
    const lane = makeLane();
    await request("POST", `/api/lanes/${lane.id}/features/activate`, { slug: "one" });
    await request("POST", `/api/lanes/${lane.id}/stage`, { stage: "review" });
    await request("POST", `/api/lanes/${lane.id}/features/activate`, { slug: "two" });

    const res = await request("GET", `/api/lanes/${lane.id}/features/one`);
    assert.equal(res.body.feature.stage, "review");
    assert.notEqual(res.body.feature.archived_at, null);
  });
});

describe("DELETE /api/lanes/:id cascades to its features", () => {
  it("removes every lane_features row for a deleted lane", async () => {
    const lane = makeLane();
    await request("POST", `/api/lanes/${lane.id}/features/activate`, { slug: "one" });
    await request("DELETE", `/api/lanes/${lane.id}`);
    const { db } = require("../db");
    const rows = db.prepare("SELECT * FROM lane_features WHERE lane_id = ?").all(lane.id);
    assert.equal(rows.length, 0);
  });
});
