import type { Product } from "@/types/product";

/**
 * The flattened, JSON-serialisable shape that `GET /api/search` returns for each
 * hit: the product fields the card renders plus its ranking signals.
 *
 * Declared in its own module so client components can import the type without
 * pulling in the server-side search implementation.
 */
export interface ProductResult extends Product {
  /** Final blended relevance score, 0..1. */
  score: number;
  /** Cosine similarity between the query vector and the product vector, 0..1. */
  similarity: number;
  /** Normalised keyword relevance, 0..1. */
  keywordScore: number;
  /** Price intent component, 0..1. */
  priceScore: number;
  /** Query terms that matched lexically. */
  matchedTerms: string[];
}

export interface SearchMetaResult {
  total: number;
  returned: number;
  took: number;
  provider: string;
  providerFallback: boolean;
  sort: string;
  weights: { semantic: number; keyword: number; price: number };
  priceIntent: "none" | "budget" | "premium";
}

export interface SearchApiResponse {
  query: string;
  count: number;
  results: ProductResult[];
  suggestions: string[];
  meta: SearchMetaResult;
}

export interface SearchApiError {
  error: { code: string; message: string; details?: Record<string, string[]> };
}
