import { useQuery } from '@tanstack/react-query';
import {
  duAnApi,
  type SearchProjectsParams,
} from '@/lib/du-an-api';

export const projectKeys = {
  all: ['projects'] as const,
  search: (params: SearchProjectsParams) => ['projects', 'search', params] as const,
  detail: (slug: string) => ['projects', 'detail', slug] as const,
  linkedListings: (projectId: string, page: number) =>
    ['projects', 'listings', projectId, page] as const,
};

export function useProjectsSearch(params: SearchProjectsParams = {}) {
  return useQuery({
    queryKey: projectKeys.search(params),
    queryFn: () => duAnApi.search(params),
  });
}

export function useProjectDetail(slug: string) {
  return useQuery({
    queryKey: projectKeys.detail(slug),
    queryFn: () => duAnApi.getBySlug(slug),
    enabled: !!slug,
  });
}

export function useProjectLinkedListings(
  projectId: string,
  page: number = 1,
) {
  return useQuery({
    queryKey: projectKeys.linkedListings(projectId, page),
    queryFn: () => duAnApi.getLinkedListings(projectId, { page, limit: 12 }),
    enabled: !!projectId,
  });
}
