import request from "supertest";
import { describe, expect, it } from "vitest";
import { createApp } from "../src/server";

describe("POST /api/normalize", () => {
  it("returns normalized v1 payload in mock mode", async () => {
    const app = createApp();
    const response = await request(app).post("/api/normalize").send({
      useMock: true,
      promptVersion: "normalizer_v1_1_2_1",
      userQuestion: "По каким поставщикам не бьются взаиморасчеты по 60 счету?"
    });

    expect(response.status).toBe(200);
    expect(response.body.trace_id).toBeTypeOf("string");
    expect(response.body.schema_version).toBe("v1");
    expect(response.body.validation?.passed).toBe(true);
    expect(response.body.normalized?.schema_version).toBe("normalized_query_v1");
  });

  it("returns normalized v2 payload in mock mode", async () => {
    const app = createApp();
    const response = await request(app).post("/api/normalize").send({
      useMock: true,
      promptVersion: "normalizer_v2",
      userQuestion: "Проверь хвосты по поставщикам и отдельно все, что не относится к данным компании."
    });

    expect(response.status).toBe(200);
    expect(response.body.trace_id).toBeTypeOf("string");
    expect(response.body.schema_version).toBe("v2");
    expect(response.body.validation?.passed).toBe(true);
    expect(response.body.normalized?.schema_version).toBe("normalized_query_v2");
    expect(Array.isArray(response.body.normalized?.fragments)).toBe(true);
  });

  it("returns normalized v2.0.1 payload in mock mode with execution_readiness", async () => {
    const app = createApp();
    const response = await request(app).post("/api/normalize").send({
      useMock: true,
      promptVersion: "normalizer_v2_0_1",
      userQuestion: "Покажи, что висит по 97 и что выглядит подозрительно."
    });

    expect(response.status).toBe(200);
    expect(response.body.trace_id).toBeTypeOf("string");
    expect(response.body.schema_version).toBe("v2_0_1");
    expect(response.body.validation?.passed).toBe(true);
    expect(response.body.normalized?.schema_version).toBe("normalized_query_v2_0_1");
    expect(Array.isArray(response.body.normalized?.fragments)).toBe(true);
    expect(response.body.normalized?.fragments?.[0]?.execution_readiness).toBeTypeOf("string");
  });

  it("returns normalized v2.0.2 payload in mock mode with route_status and no_route_reason", async () => {
    const app = createApp();
    const response = await request(app).post("/api/normalize").send({
      useMock: true,
      promptVersion: "normalizer_v2_0_2",
      userQuestion: "Проверь 97 и покажи, где логика учета выглядит подозрительно."
    });

    expect(response.status).toBe(200);
    expect(response.body.trace_id).toBeTypeOf("string");
    expect(response.body.schema_version).toBe("v2_0_2");
    expect(response.body.validation?.passed).toBe(true);
    expect(response.body.normalized?.schema_version).toBe("normalized_query_v2_0_2");
    expect(Array.isArray(response.body.normalized?.fragments)).toBe(true);
    expect(response.body.normalized?.fragments?.[0]?.route_status).toBeTypeOf("string");
    expect(response.body.normalized?.fragments?.[0]?.no_route_reason).toBeDefined();
  });
});
