/* @vitest-environment node */

import { describe, it, expect } from "vitest";
import { ApiClientError } from "@/lib/frontend/apiClient";
import { buildDateFilterValidationError } from "./dateFilterValidation.js";

describe("lib/frontend/search/dateFilterValidation", () => {
	it("returns null when no dates are set", () => {
		expect(buildDateFilterValidationError({ from: null, to: null })).toBe(null);
		expect(buildDateFilterValidationError({})).toBe(null);
	});

	it("returns null for a valid open range", () => {
		expect(
			buildDateFilterValidationError({ from: "2026-01-01", to: null })
		).toBe(null);
		expect(
			buildDateFilterValidationError({ from: null, to: "2026-01-31" })
		).toBe(null);
	});

	it("returns null for a valid range", () => {
		expect(
			buildDateFilterValidationError({ from: "2026-01-01", to: "2026-01-31" })
		).toBe(null);
	});

	it("accepts from===to (single-day)", () => {
		expect(
			buildDateFilterValidationError({ from: "2026-01-14", to: "2026-01-14" })
		).toBe(null);
	});

	it("returns VALIDATION_SEARCH_RANGE when from > to", () => {
		const err = buildDateFilterValidationError({
			from: "2026-01-14",
			to: "2026-01-13",
		});

		expect(err).toBeInstanceOf(ApiClientError);
		expect(err.code).toBe("VALIDATION_SEARCH_RANGE");
	});

	it("returns VALIDATION_SEARCH_DATE for invalid from", () => {
		const err = buildDateFilterValidationError({
			from: "2026/01/01",
			to: null,
		});

		expect(err).toBeInstanceOf(ApiClientError);
		expect(err.code).toBe("VALIDATION_SEARCH_DATE");
	});

	it("returns VALIDATION_SEARCH_DATE for invalid to", () => {
		const err = buildDateFilterValidationError({
			from: null,
			to: "2026-99-01",
		});

		expect(err).toBeInstanceOf(ApiClientError);
		expect(err.code).toBe("VALIDATION_SEARCH_DATE");
	});
});
