import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
  valuationApi,
  type ValuationRequest,
  type BatchValuationRequest,
} from '@/lib/valuation-api';

export const valuationKeys = {
  all: ['valuation'] as const,
  history: (page: number) => ['valuation', 'history', page] as const,
  detail: (id: string) => ['valuation', 'detail', id] as const,
  propertyHistory: (propertyId: string) =>
    ['valuation', 'property-history', propertyId] as const,
  compare: (ids: string[]) => ['valuation', 'compare', ...ids] as const,
  projectSearch: (query: string) =>
    ['valuation', 'project-search', query] as const,
};

export function useValuationPredict() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: ValuationRequest) => valuationApi.predict(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: valuationKeys.all });
    },
  });
}

export function useValuationPredictForListing() {
  return useMutation({
    mutationFn: (listingId: string) => valuationApi.predictForListing(listingId),
  });
}

export function useValuationBatch() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: BatchValuationRequest) => valuationApi.batchPredict(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: valuationKeys.all });
    },
  });
}

export function useValuationCompare(propertyIds: string[]) {
  return useQuery({
    queryKey: valuationKeys.compare(propertyIds),
    queryFn: () => valuationApi.compare({ propertyIds }),
    enabled: propertyIds.length >= 2,
  });
}

export function useValuationHistory(page = 1) {
  return useQuery({
    queryKey: valuationKeys.history(page),
    queryFn: () => valuationApi.getHistory(page),
  });
}

export function useValuationPropertyHistory(propertyId: string) {
  return useQuery({
    queryKey: valuationKeys.propertyHistory(propertyId),
    queryFn: () => valuationApi.getPropertyHistory(propertyId),
    enabled: !!propertyId,
  });
}

export function useValuationDetail(id: string) {
  return useQuery({
    queryKey: valuationKeys.detail(id),
    queryFn: () => valuationApi.getById(id),
    enabled: !!id,
  });
}

export function useProjectSearch(query: string) {
  return useQuery({
    queryKey: valuationKeys.projectSearch(query),
    queryFn: () => valuationApi.searchProjects(query),
    enabled: query.length >= 2,
    staleTime: 30_000,
  });
}
