import { baseApi } from "@/store/api/baseApi";

export type SearchType = "user" | "admin" | "todo";

export interface SearchResult {
  id: string;
  type: SearchType;
  title: string;
  subtitle?: string;
  description?: string;
  createdAt?: string;
  score: number;
  meta?: Record<string, any>;
}

export interface QuickSearchResponse {
  results: SearchResult[];
}

export interface SearchResponse {
  results: SearchResult[];
  total: number;
  page: number;
  limit: number;
}

export const searchApi = baseApi.injectEndpoints({
  overrideExisting: true,
  endpoints: (builder) => ({
    quickSearch: builder.query<
      QuickSearchResponse,
      { q: string; limit?: number }
    >({
      query: ({ q, limit = 8 }) => ({
        url: "/search/quick",
        params: { q, limit },
      }),
    }),

    globalSearch: builder.query<
      SearchResponse,
      { q: string; page?: number; limit?: number }
    >({
      query: ({ q, page = 1, limit = 25 }) => ({
        url: "/search",
        params: { q, page, limit },
      }),
    }),
  }),
});

export const { useLazyQuickSearchQuery, useGlobalSearchQuery } = searchApi;
