import { describe, it, expect } from "vitest";
import { PgDialect } from "drizzle-orm/pg-core";
import { buildFilterExists, buildNameCondition } from "@/lib/search/query-vehicles";
import { vehicles } from "@/db/schema/assets";
import type { FilterValue } from "@/lib/search/types";

const dialect = new PgDialect();
const TANK = "11111111-1111-1111-1111-111111111111";
const ALLRAD = "22222222-2222-2222-2222-222222222222";
const PUMPE = "33333333-3333-3333-3333-333333333333";

function render(query: ReturnType<typeof buildFilterExists>) {
  const { sql, params } = dialect.sqlToQuery(query);
  return { sql, params };
}

describe("buildNameCondition", () => {
  it("liefert undefined ohne Suchtext", () => {
    expect(buildNameCondition(undefined)).toBeUndefined();
    expect(buildNameCondition("   ")).toBeUndefined();
  });

  it("expandiert HLFA 3 zu HLF 3 (Allrad) und sucht auf name + funkrufname", () => {
    const cond = buildNameCondition("HLFA 3");
    expect(cond).toBeDefined();
    const { sql, params } = render(cond!);
    expect(sql).toContain("ilike");
    // 2 Varianten (HLFA 3, HLF 3) x 2 Spalten (name, funkrufname) = 4 Patterns
    expect(params).toContain("%HLFA 3%");
    expect(params).toContain("%HLF 3%");
    expect(params.filter((p) => p === "%HLF 3%").length).toBe(2);
  });
});

describe("buildFilterExists", () => {
  it("number gte/lte erzeugt EXISTS mit value_num Range", () => {
    const f: FilterValue = { typ: "number", merkmalId: TANK, gte: 2000, lte: 4000 };
    const { sql, params } = render(buildFilterExists(f, "vehicle", vehicles.id));
    expect(sql.toLowerCase()).toContain("exists");
    expect(sql.toLowerCase()).toContain("value_num");
    expect(params).toContain(TANK);
    expect(params).toContain(2000);
    expect(params).toContain(4000);
  });

  it("number nur gte erzeugt nur untere Grenze", () => {
    const f: FilterValue = { typ: "number", merkmalId: TANK, gte: 2000 };
    const { params } = render(buildFilterExists(f, "vehicle", vehicles.id));
    expect(params).toContain(2000);
    expect(params).not.toContain(4000);
  });

  it("boolean erzeugt value_bool-Gleichheit", () => {
    const f: FilterValue = { typ: "boolean", merkmalId: ALLRAD, eq: true };
    const { sql, params } = render(buildFilterExists(f, "vehicle", vehicles.id));
    expect(sql.toLowerCase()).toContain("value_bool");
    expect(params).toContain(ALLRAD);
    expect(params).toContain(true);
  });

  it("enum erzeugt value_text IN (...)", () => {
    const f: FilterValue = { typ: "enum", merkmalId: PUMPE, in: ["fpn1000", "fpn2000"] };
    const { sql, params } = render(buildFilterExists(f, "vehicle", vehicles.id));
    expect(sql.toLowerCase()).toContain("value_text");
    expect(params).toContain("fpn1000");
    expect(params).toContain("fpn2000");
  });

  it("setzt den richtigen entity_typ", () => {
    const f: FilterValue = { typ: "boolean", merkmalId: ALLRAD, eq: false };
    const { params } = render(buildFilterExists(f, "equipment", vehicles.id));
    expect(params).toContain("equipment");
  });
});
