import { useState, useCallback, useEffect } from 'react';
import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';
import { IMAGE_UPLOAD_METHODS } from '../../constants';

/**
 * Custom hook for managing image/asset upload logic
 * @param {Object} params - Hook parameters
 * @param {Object} params.webPush - WebPush state from Redux
 * @param {Object} params.webPushActions - WebPush action creators
 * @param {number} params.index - Asset index (0 for image, 1 for brand icon)
 * @param {string} params.initialUploadMethod - Initial upload method
 * @returns {Object} Image upload state and handlers
 */
export const useImageUpload = ({
  webPush,
  webPushActions,
  index,
  initialUploadMethod = IMAGE_UPLOAD_METHODS.UPLOAD_IMAGE,
}) => {
  const [uploadMethod, setUploadMethod] = useState(initialUploadMethod);
  const [imageSrc, setImageSrc] = useState('');
  const [imageUrl, setImageUrl] = useState('');
  const [isValidating, setIsValidating] = useState(false);
  const [isUploading, setIsUploading] = useState(false);

  const handleUploadMethodChange = useCallback((e) => {
    const method = e.target.value;
    setUploadMethod(method);
    setImageSrc('');
    setImageUrl('');
  }, []);

  const setUpdateImageSrc = useCallback(
    (filePath) => {
      setImageSrc(filePath);
      webPushActions.clearWebPushAsset(index);
    },
    [webPushActions, index],
  );

  const updateOnReUpload = useCallback(() => {
    setImageSrc('');
  }, []);

  const uploadAsset = useCallback(
    (file, type, fileParams) => {
      webPushActions.uploadWebPushAsset(file, type, fileParams, index);
    },
    [webPushActions, index],
  );

  const handleUrlChange = useCallback(
    (url) => {
      setImageUrl(url);
      // Clear imageSrc when URL changes
      if (url !== imageUrl) {
        setImageSrc('');
      }
    },
    [imageUrl],
  );

  const handleValidationStateChange = useCallback((validating) => {
    setIsValidating(validating);
  }, []);

  const handleUploadStateChange = useCallback((uploading) => {
    setIsUploading(uploading);
  }, []);

  // Restore image from uploaded asset data
  useEffect(() => {
    const assetDataKey = index === 0 ? 'uploadedAssetData0' : `uploadedAssetData${index}`;
    const imageDataObj = get(webPush, assetDataKey) || (index === 0 ? get(webPush, 'uploadedAssetData') : null);
    if (!isEmpty(imageDataObj)) {
      const { secure_file_path = '' } = get(imageDataObj, 'metaInfo', {});
      if (secure_file_path) {
        setImageSrc(secure_file_path);
      }
    }
    // Note: Not clearing imageSrc when asset data is missing for edit mode compatibility
    // Excluding setImageSrc from deps to prevent infinite loops; it's a stable setter that doesn't need re-runs
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [webPush, index]);

  return {
    uploadMethod,
    imageSrc,
    imageUrl,
    isValidating,
    isUploading,
    setImageSrc,
    setImageUrl,
    handleUploadMethodChange,
    setUpdateImageSrc,
    updateOnReUpload,
    uploadAsset,
    handleUrlChange,
    handleValidationStateChange,
    handleUploadStateChange,
  };
};

