import {
	isInvalidIsoDateRange,
	isValidIsoDateYmd,
} from "@/lib/frontend/search/dateRange";

/**
 * Centralized validation for search date filters (ISO YYYY-MM-DD).
 *
 * Returns a small, UI-agnostic error descriptor (or null).
 *
 * Notes:
 * - from === to is valid (single-day search)
 * - from > to is invalid
 *
 * @param {string|null} from
 * @param {string|null} to
 * @returns {{code: string, message: string, details?: any} | null}
 */
export function getSearchDateRangeValidation(from, to) {
	if (from && !isValidIsoDateYmd(from)) {
		return {
			code: "VALIDATION_SEARCH_DATE",
			message: "Invalid from date",
			details: { from },
		};
	}

	if (to && !isValidIsoDateYmd(to)) {
		return {
			code: "VALIDATION_SEARCH_DATE",
			message: "Invalid to date",
			details: { to },
		};
	}

	if (isInvalidIsoDateRange(from, to)) {
		return {
			code: "VALIDATION_SEARCH_RANGE",
			message: "Invalid date range",
			details: { from, to },
		};
	}

	return null;
}
