import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { assetsApi, Asset } from '@/services/api/assets.api';
import { PaginationParams } from '@/services/api/client';
import { useToast } from '@/hooks/use-toast';

export function useAssets(
  params?: PaginationParams & { brandId?: string; type?: string; search?: string }
) {
  return useQuery({
    queryKey: ['assets', params],
    queryFn: () => assetsApi.getAll(params).then((res) => res.data),
  });
}

export function useAsset(id: string) {
  return useQuery({
    queryKey: ['assets', id],
    queryFn: () => assetsApi.getById(id).then((res) => res.data),
    enabled: !!id,
  });
}

export function useAssetsByBrand(brandId: string) {
  return useQuery({
    queryKey: ['assets', 'brand', brandId],
    queryFn: () => assetsApi.getByBrand(brandId).then((res) => res.data),
    enabled: !!brandId,
  });
}

export function useCreateAsset() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (data: Partial<Asset>) => assetsApi.create(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['assets'] });
      toast({ title: 'Asset creado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al crear asset',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useUpdateAsset() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<Asset> }) =>
      assetsApi.update(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['assets'] });
      toast({ title: 'Asset actualizado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al actualizar asset',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useDeleteAsset() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (id: string) => assetsApi.delete(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['assets'] });
      toast({ title: 'Asset eliminado exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al eliminar asset',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}

export function useBulkDeleteAssets() {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: (ids: string[]) => assetsApi.bulkDelete(ids),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['assets'] });
      toast({ title: 'Assets eliminados exitosamente' });
    },
    onError: (error: any) => {
      toast({
        title: 'Error al eliminar assets',
        description: error.response?.data?.message || 'Error desconocido',
        variant: 'destructive',
      });
    },
  });
}
