import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { api } from "@/lib/api";
import { queryKeys } from "@/lib/queryKeys";
import type { InvoiceStatus } from "@rubis/shared";

/**
 * Forme minimale renvoyée par GET /api/v1/checkin/inapp/pending — basée sur
 * InvoiceTransformer côté API. On ne garde que ce dont la modale a besoin.
 */
export type PendingCheckinInvoice = {
  id: string;
  numero: string;
  amountTtcCents: number;
  issueDate: string;
  dueDate: string;
  status: InvoiceStatus;
  clientName: string;
  planName: string | null;
};

/** Liste des factures en attente de check-in pour l'org courante. */
export function usePendingCheckins() {
  return useQuery({
    queryKey: queryKeys.checkin.pending(),
    queryFn: () =>
      api.get<PendingCheckinInvoice[]>("/api/v1/checkin/inapp/pending"),
    // Pas de polling — la liste change uniquement quand l'user répond ou
    // qu'une nouvelle invoice arrive en awaiting_user_confirmation. On
    // refetch sur mount + sur invalidate.
    staleTime: 30_000,
  });
}

/** Mutation : "oui, payée" — délègue à l'endpoint inappRespondPaid. */
export function useCheckinPaid() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (invoiceId: string) =>
      api.post(`/api/v1/checkin/inapp/${invoiceId}/paid`),
    onSuccess: () => {
      // Tout l'écosystème dépend du statut : invalidate large (factures,
      // dashboard KPIs, timeseries, pipeline, counts).
      void qc.invalidateQueries({ queryKey: queryKeys.checkin.pending() });
      void qc.invalidateQueries({ queryKey: queryKeys.invoices.all() });
      void qc.invalidateQueries({ queryKey: queryKeys.dashboard.all() });
    },
  });
}

/** Mutation : "non, toujours impayée" — programme les relances. */
export function useCheckinStillPending() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: (invoiceId: string) =>
      api.post(`/api/v1/checkin/inapp/${invoiceId}/pending`),
    onSuccess: () => {
      void qc.invalidateQueries({ queryKey: queryKeys.checkin.pending() });
      void qc.invalidateQueries({ queryKey: queryKeys.invoices.all() });
      void qc.invalidateQueries({ queryKey: queryKeys.dashboard.all() });
    },
  });
}
