import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useAuth0 } from '@auth0/auth0-react';
import { subscriptionApi } from '../api/subscription.api';
import toast from 'react-hot-toast';

export const useSubscription = () => {
  const { isAuthenticated, isLoading } = useAuth0();

  return useQuery({
    queryKey: ['subscription'],
    queryFn: () => subscriptionApi.getSubscription(),
    enabled: isAuthenticated && !isLoading,
    staleTime: 5 * 60 * 1000,
    retry: (failureCount, error: unknown) => {
      const err = error as { response?: { status?: number } };
      if (err?.response?.status === 401 && failureCount < 3) return true;
      return false;
    },
  });
};

export const useNeedsVehicleSelection = () => {
  const { isAuthenticated, isLoading } = useAuth0();

  return useQuery({
    queryKey: ['needs-vehicle-selection'],
    queryFn: () => subscriptionApi.needsVehicleSelection(),
    enabled: isAuthenticated && !isLoading,
    staleTime: 0, // Always fetch fresh on login
  });
};

export const useCheckout = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.checkout,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
      toast.success('Subscription upgraded successfully');
    },
    onError: (error: unknown) => {
      const err = error as { response?: { data?: { error?: string } } };
      toast.error(err.response?.data?.error || 'Failed to upgrade subscription');
    },
  });
};

export const useCancelSubscription = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.cancel,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
      toast.success('Subscription scheduled for cancellation');
    },
    onError: (error: unknown) => {
      const err = error as { response?: { data?: { error?: string } } };
      toast.error(err.response?.data?.error || 'Failed to cancel subscription');
    },
  });
};

export const useReactivateSubscription = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.reactivate,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
      toast.success('Subscription reactivated');
    },
    onError: (error: unknown) => {
      const err = error as { response?: { data?: { error?: string } } };
      toast.error(err.response?.data?.error || 'Failed to reactivate subscription');
    },
  });
};

export const useInvoices = () => {
  const { isAuthenticated, isLoading } = useAuth0();
  return useQuery({
    queryKey: ['invoices'],
    queryFn: () => subscriptionApi.getInvoices(),
    enabled: isAuthenticated && !isLoading,
    staleTime: 5 * 60 * 1000,
  });
};

export const useDowngrade = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.downgrade,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
      queryClient.invalidateQueries({ queryKey: ['vehicles'] });
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
      toast.success('Subscription downgraded successfully');
    },
    onError: (error: unknown) => {
      const err = error as { response?: { data?: { message?: string } } };
      toast.error(err.response?.data?.message || 'Downgrade failed');
    },
  });
};

export const useCreateDonation = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (amount: number) => subscriptionApi.createDonation(amount),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['donations'] });
    },
    onError: (error: unknown) => {
      const err = error as { response?: { data?: { error?: string } } };
      toast.error(err.response?.data?.error || 'Donation failed');
    },
  });
};

export const useDonations = () => {
  const { isAuthenticated, isLoading } = useAuth0();
  return useQuery({
    queryKey: ['donations'],
    queryFn: () => subscriptionApi.getDonations(),
    enabled: isAuthenticated && !isLoading,
    staleTime: 5 * 60 * 1000,
  });
};
