import { useCallback } from 'react';
import { useRowSelection, UseRowSelectionResult } from './useRowSelection';
import {
  useBatchUpdate,
  UseBatchUpdateConfig,
  BatchUpdateResult,
} from './useBatchUpdate';
import {
  useBatchDelete,
  UseBatchDeleteConfig,
  BatchDeleteResult,
} from './useBatchDelete';

export interface UseBatchOperationsConfig
  extends
    Omit<UseBatchUpdateConfig, 'onComplete' | 'onSuccess' | 'onError'>,
    Omit<
      UseBatchDeleteConfig,
      'onComplete' | 'onSuccess' | 'onError' | 'resourceName'
    > {
  /** Called after batch update completes */
  onUpdateComplete?: (result: BatchUpdateResult) => void;
  /** Called after batch delete completes */
  onDeleteComplete?: (result: BatchDeleteResult) => void;
  /** Called when any batch operation succeeds (for refetch, etc.) */
  onSuccess?: () => void;
}

export interface UseBatchOperationsResult extends UseRowSelectionResult {
  /** Update selected items */
  updateSelectedItems: (values: any) => Promise<void>;
  /** Delete selected items */
  deleteSelectedItems: () => Promise<void>;
  /** Update a single item */
  updateOne: (id: number, values: any) => Promise<void>;
  /** Delete a single item */
  deleteOne: (id: number) => Promise<void>;
  /** Update loading state */
  updateManyIsLoading: boolean;
  /** Delete loading state */
  deleteManyIsLoading: boolean;
}

/**
 * Combined hook for all batch operations (selection, update, delete)
 *
 * @example
 * // Basic usage
 * const batchOps = useBatchOperations({
 *   resourceName: 'audit',
 *   apiUrl: '/api',
 *   onSuccess: () => refetch(),
 * });
 *
 * @example
 * // With custom handlers
 * const batchOps = useBatchOperations({
 *   resourceName: 'audit',
 *   apiUrl: '/api',
 *   showNotification: false,
 *   onUpdateComplete: (result) => {
 *     console.log('Updated:', result.successCount);
 *   },
 *   onDeleteComplete: (result) => {
 *     console.log('Deleted:', result.successCount);
 *   },
 * });
 */
export function useBatchOperations({
  resourceName,
  apiUrl,
  requestHeaders,
  onSuccess,
  onUpdateComplete,
  onDeleteComplete,
  t,
  showNotification,
  customNotification,
  customUpdateFn,
  customDeleteFn,
  confirmDelete,
}: UseBatchOperationsConfig): UseBatchOperationsResult {
  const {
    selectedRowKeys,
    setSelectedRowKeys,
    rowSelection,
    hasSelected,
    clearSelection,
  } = useRowSelection();

  const {
    updateSelectedItems: batchUpdate,
    updateOne,
    updateManyIsLoading,
  } = useBatchUpdate({
    resourceName,
    apiUrl,
    requestHeaders,
    t,
    showNotification,
    customNotification: customNotification as any,
    customUpdateFn,
    onComplete: onUpdateComplete,
    onSuccess: onSuccess ? () => onSuccess() : undefined,
  });

  const {
    deleteSelectedItems: batchDelete,
    deleteOne,
    deleteManyIsLoading,
  } = useBatchDelete({
    resourceName,
    t,
    showNotification,
    customNotification: customNotification as any,
    customDeleteFn,
    confirmDelete,
    onComplete: onDeleteComplete,
    onSuccess: onSuccess ? () => onSuccess() : undefined,
  });

  const updateSelectedItems = useCallback(
    async (values: any) => {
      const failedIds = await batchUpdate(selectedRowKeys, values);
      setSelectedRowKeys(failedIds);
    },
    [batchUpdate, selectedRowKeys, setSelectedRowKeys],
  );

  const deleteSelectedItems = useCallback(async () => {
    const failedIds = await batchDelete(selectedRowKeys);
    setSelectedRowKeys(failedIds);
  }, [batchDelete, selectedRowKeys, setSelectedRowKeys]);

  return {
    // Selection
    selectedRowKeys,
    setSelectedRowKeys,
    rowSelection,
    hasSelected,
    clearSelection,

    // Operations
    updateSelectedItems,
    deleteSelectedItems,
    updateOne,
    deleteOne,

    // Loading states
    updateManyIsLoading,
    deleteManyIsLoading,
  };
}
