'use client';

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import * as subscriptionApi from '../api/subscription';

export function useSubscription(tenantId: string | undefined) {
  return useQuery({
    queryKey: ['subscription', tenantId],
    queryFn: () => subscriptionApi.getSubscription(tenantId!),
    enabled: !!tenantId,
    staleTime: 5 * 60 * 1000,
  });
}

export function usePaymentHistory(tenantId: string | undefined) {
  return useQuery({
    queryKey: ['payments', tenantId],
    queryFn: () => subscriptionApi.getPaymentHistory(tenantId!),
    enabled: !!tenantId,
    staleTime: 60 * 1000,
  });
}

export function useGeneratePaymentLink() {
  return useMutation({
    mutationFn: (tenantId: string) => subscriptionApi.generatePaymentLink(tenantId),
  });
}

export function useMarkAsPaid() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ tenantId, amount }: { tenantId: string; amount: number }) =>
      subscriptionApi.markAsPaid(tenantId, amount),
    onSuccess: (_, { tenantId }) => {
      queryClient.invalidateQueries({ queryKey: ['subscription', tenantId] });
      queryClient.invalidateQueries({ queryKey: ['payments', tenantId] });
    },
  });
}

// ============================================================================
// Self-serve hooks (actúan sobre el tenant del usuario autenticado)
// ============================================================================

export function usePlans() {
  return useQuery({
    queryKey: ['subscription-plans'],
    queryFn: subscriptionApi.getPlans,
    staleTime: 10 * 60 * 1000, // 10 min — los precios cambian poco
  });
}

export function useStartTrial() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.startTrial,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}

export function useSubscribeMe() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.subscribeMe,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}

export function useChangeMyPlan() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.changeMyPlan,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}

export function useCancelMySubscription() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.cancelMySubscription,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}

export function useUpdatePlanPrice() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, amount }: { id: number; amount: number }) =>
      subscriptionApi.updatePlanPrice(id, amount),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription-plans'] });
    },
  });
}

export function useUpgradeMe() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (plan: string) => subscriptionApi.upgradeMe(plan),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}

export function useCancelPendingUpgrade() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.cancelPendingUpgrade,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}

export function useReactivateMe() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: subscriptionApi.reactivateMe,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['subscription'] });
    },
  });
}
