import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { couponsService } from "./service";
import type {
  CreateCouponDto,
  SuggestCouponDto,
  AnalyzeMatchDto,
  FrequencyCouponRequestDto,
} from "./types";

export const CouponsQueryKeys = {
  all: ["coupons"] as const,
  history: (limit?: number) =>
    [...CouponsQueryKeys.all, "history", limit] as const,
  stats: () => [...CouponsQueryKeys.all, "stats"] as const,
};

export const useAnalyzeMatch = () => {
  return useMutation({
    mutationFn: (dto: AnalyzeMatchDto) => couponsService.analyzeMatch(dto),
  });
};

export const useCreateCoupon = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (dto: CreateCouponDto) => couponsService.createCoupon(dto),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: CouponsQueryKeys.all });
    },
  });
};

export const useDailyBanko = () => {
  return useMutation({
    mutationFn: (matchIds: string[]) => couponsService.getDailyBanko(matchIds),
  });
};

export const useCouponHistory = (limit: number = 20) => {
  return useQuery({
    queryKey: CouponsQueryKeys.history(limit),
    queryFn: () => couponsService.getHistory(limit),
  });
};

export const useUserBettingStats = () => {
  return useQuery({
    queryKey: CouponsQueryKeys.stats(),
    queryFn: () => couponsService.getUserStats(),
  });
};

export const useSuggestCoupon = () => {
  return useMutation({
    mutationFn: (dto: SuggestCouponDto) => couponsService.suggestCoupon(dto),
  });
};

export const useGenerateFrequencyCoupon = () => {
  return useMutation({
    mutationFn: (dto: FrequencyCouponRequestDto) =>
      couponsService.generateFrequencyCoupon(dto),
  });
};
