// src/hooks/useDataExtraction.ts
import { useCallback } from 'react';
import type { Flyer } from '../types';

type ExtractionType = 'store_name' | 'dates';

interface UseDataExtractionOptions {
  selectedFlyer: Flyer | null;
  onFlyerUpdate: (flyer: Flyer) => void;
}

interface UseDataExtractionReturn {
  handleDataExtracted: (type: ExtractionType, value: string) => void;
}

/**
 * A custom hook to handle data extraction from the correction tool.
 * Updates the selected flyer with extracted store name or date information.
 *
 * Note: This currently only updates local state for immediate visual feedback.
 * A production implementation should also persist changes to the database.
 *
 * @param options.selectedFlyer - The currently selected flyer
 * @param options.onFlyerUpdate - Callback to update the flyer state
 * @returns Object with handleDataExtracted callback
 *
 * @example
 * ```tsx
 * const { handleDataExtracted } = useDataExtraction({
 *   selectedFlyer,
 *   onFlyerUpdate: setSelectedFlyer,
 * });
 * ```
 */
export const useDataExtraction = ({
  selectedFlyer,
  onFlyerUpdate,
}: UseDataExtractionOptions): UseDataExtractionReturn => {
  const handleDataExtracted = useCallback(
    (type: ExtractionType, value: string) => {
      if (!selectedFlyer) return;

      // Create an updated copy of the flyer
      const updatedFlyer = { ...selectedFlyer };

      if (type === 'store_name') {
        updatedFlyer.store = { ...updatedFlyer.store!, name: value };
      } else if (type === 'dates') {
        // A more robust solution would parse the date string properly.
        // For now, this is a placeholder for future date extraction logic.
      }

      onFlyerUpdate(updatedFlyer);
    },
    [selectedFlyer, onFlyerUpdate],
  );

  return {
    handleDataExtracted,
  };
};
