/**
 * API Keys Hook
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { listApiKeys, createApiKey, deleteApiKey, type ApiKey } from '@/lib/api/userApiKeys';

export const useApiKeys = () => {
  const queryClient = useQueryClient();

  const listQuery = useQuery<ApiKey[]>({
    queryKey: ['api-keys'],
    queryFn: () => listApiKeys(),
  });

  return {
    // Raw query/mutation objects
    list: listQuery,
    create: useMutation({
      mutationFn: createApiKey,
      onSuccess: () => queryClient.invalidateQueries({ queryKey: ['api-keys'] }),
    }),
    delete: useMutation({
      mutationFn: deleteApiKey,
      onSuccess: () => queryClient.invalidateQueries({ queryKey: ['api-keys'] }),
    }),
    // Compatible interface for legacy code
    apiKeys: listQuery.data || [],
    isLoading: listQuery.isLoading,
    error: listQuery.error,
    refresh: listQuery.refetch,
  };
};
