import { describe, it, expect } from "vitest";
import { GET } from "./route";

const MODEL_ID = "verkoopovereenkomst-compromis";

function maakRequest(query = ""): Request {
  return new Request(`http://localhost/api/modeldocumenten/${MODEL_ID}${query}`);
}

function maakParams(id = MODEL_ID): { params: Promise<{ id: string }> } {
  return { params: Promise.resolve({ id }) };
}

describe("GET /api/modeldocumenten/[id]", () => {
  it("zonder queryparameters: het sjabloon, parameters en structuur (geen ontwerp)", async () => {
    const res = await GET(maakRequest(), maakParams());
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json.id).toBe(MODEL_ID);
    expect(typeof json.tekst).toBe("string");
    expect(Array.isArray(json.parameters)).toBe(true);
    expect(Array.isArray(json.structuur)).toBe(true);
    expect(json.ontwerp).toBeUndefined();
  });

  it("elke parameter is een invulhulp met niet-lege omschrijving, voorbeeld en herkomst", async () => {
    const res = await GET(maakRequest(), maakParams());
    const json = await res.json();
    expect(json.parameters.length).toBeGreaterThan(0);
    for (const p of json.parameters) {
      expect(p.naam).toBeTruthy();
      expect(p.omschrijving?.trim()).toBeTruthy();
      expect(p.voorbeeld?.trim()).toBeTruthy();
      expect(["gedeclareerd", "catalogus", "afgeleid"]).toContain(p.herkomst);
    }
  });

  it("404 bij onbekend model-id", async () => {
    const res = await GET(maakRequest(), maakParams("bestaat-niet"));
    expect(res.status).toBe(404);
  });

  it("mét parameterwaarden: 'ontwerp' bevat de ingevulde waarden en geen rauwe {{parameter}}", async () => {
    const res = await GET(
      maakRequest("?prijs_letters=DRIEHONDERDDUIZEND&prijs_cijfers=300000"),
      maakParams()
    );
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(typeof json.ontwerp).toBe("string");
    expect(json.ontwerp).toContain("DRIEHONDERDDUIZEND");
    expect(json.ontwerp).toContain("300000");
    // De ingevulde parameters mogen niet meer als {{...}} in het ontwerp staan.
    expect(json.ontwerp).not.toContain("{{prijs_letters}}");
    expect(json.ontwerp).not.toContain("{{prijs_cijfers}}");
    expect(json.disclaimer).toMatch(/[Ww]erkdocument/);
  });

  it("met facultatief=...: het facultatieve onderdeel komt in het ontwerp", async () => {
    const onderdeelId = "roerende-goederen-zonnepanelen";
    const res = await GET(
      maakRequest(`?facultatief=${onderdeelId}`),
      maakParams()
    );
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json.opgenomenFacultatief).toContain(onderdeelId);
    expect(json.genegeerdFacultatief).toEqual([]);
    // De titel van het facultatieve onderdeel verschijnt in het ontwerp.
    expect(json.ontwerp).toContain("ROERENDE GOEDEREN, ZONNEPANELEN EN INSTALLATIES");
  });

  it("onbekende facultatieve id belandt in 'genegeerdFacultatief'", async () => {
    const res = await GET(maakRequest("?facultatief=bestaat-niet"), maakParams());
    expect(res.status).toBe(200);
    const json = await res.json();
    expect(json.genegeerdFacultatief).toContain("bestaat-niet");
    expect(json.opgenomenFacultatief).toEqual([]);
  });
});
