// ADM-009 — TanStack Query hooks para fiscal/iva
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import {
  listTiposDeIva,
  getTipoDeIvaById,
  getHistorialTipoDeIva,
  createTipoDeIva,
  updateTipoDeIva,
  nuevaVersionTipoDeIva,
  deactivateTipoDeIva,
  reactivateTipoDeIva,
} from '../api/ivaApi'
import type {
  TipoDeIvaFilter,
  CreateTipoDeIvaRequest,
  UpdateTipoDeIvaRequest,
  NuevaVersionTipoDeIvaRequest,
} from '../types/tipoDeIva.types'

// ─── Query keys estables ──────────────────────────────────────────────────────

export const ivaListQueryKey = (filters: TipoDeIvaFilter) =>
  ['fiscal', 'iva', 'list', filters] as const

export const ivaDetailQueryKey = (id: number) =>
  ['fiscal', 'iva', id] as const

export const ivaHistorialQueryKey = (id: number) =>
  ['fiscal', 'iva', id, 'historial'] as const

// ─── List ─────────────────────────────────────────────────────────────────────

export function useTiposDeIvaList(filters: TipoDeIvaFilter) {
  return useQuery({
    queryKey: ivaListQueryKey(filters),
    queryFn: () => listTiposDeIva(filters),
    staleTime: 15_000,
  })
}

// ─── Detail ──────────────────────────────────────────────────────────────────

export function useTipoDeIva(id: number | null) {
  return useQuery({
    queryKey: ivaDetailQueryKey(id ?? 0),
    queryFn: () => getTipoDeIvaById(id!),
    enabled: id != null,
    staleTime: 15_000,
  })
}

// ─── Historial (lazy — solo cuando el tooltip está abierto) ───────────────────

export function useHistorialTipoDeIva(id: number | null, enabled = false) {
  return useQuery({
    queryKey: ivaHistorialQueryKey(id ?? 0),
    queryFn: () => getHistorialTipoDeIva(id!),
    enabled: id != null && enabled,
    staleTime: 15_000,
  })
}

// ─── Create ──────────────────────────────────────────────────────────────────

export function useCreateTipoDeIva() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (payload: CreateTipoDeIvaRequest) => createTipoDeIva(payload),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['fiscal', 'iva'] })
    },
  })
}

// ─── Update ──────────────────────────────────────────────────────────────────

export function useUpdateTipoDeIva() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: ({ id, body }: { id: number; body: UpdateTipoDeIvaRequest }) =>
      updateTipoDeIva(id, body),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['fiscal', 'iva'] })
    },
  })
}

// ─── Nueva versión ───────────────────────────────────────────────────────────

export function useNuevaVersionTipoDeIva() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: ({ id, body }: { id: number; body: NuevaVersionTipoDeIvaRequest }) =>
      nuevaVersionTipoDeIva(id, body),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['fiscal', 'iva'] })
    },
  })
}

// ─── Deactivate / Reactivate ─────────────────────────────────────────────────

export function useDeactivateTipoDeIva() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (id: number) => deactivateTipoDeIva(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['fiscal', 'iva'] })
    },
  })
}

export function useReactivateTipoDeIva() {
  const queryClient = useQueryClient()
  return useMutation({
    mutationFn: (id: number) => reactivateTipoDeIva(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['fiscal', 'iva'] })
    },
  })
}
