import { useState, useCallback, useMemo } from 'react';
import { v4 as uuidv4 } from 'uuid';
import { WEBPUSH_BUTTON_TYPES } from '../../constants';

/**
 * Custom hook for managing Web Push CTA buttons
 * @param {Array} initialButtons - Initial button data
 * @returns {Object} Button state and handlers
 */
export const useButtonManagement = (initialButtons = []) => {
  const [buttons, setButtons] = useState(initialButtons);
  const [isAddingButton, setIsAddingButton] = useState(false);
  const [buttonBeingAdded, setButtonBeingAdded] = useState(null);
  const [editingButtonIndex, setEditingButtonIndex] = useState(null);

  const handleAddPrimaryButton = useCallback(() => {
    setIsAddingButton(true);
    setButtonBeingAdded(WEBPUSH_BUTTON_TYPES.PRIMARY);
    setEditingButtonIndex(null);
  }, []);

  const handleAddSecondaryButton = useCallback(() => {
    setIsAddingButton(true);
    setButtonBeingAdded(WEBPUSH_BUTTON_TYPES.SECONDARY);
    setEditingButtonIndex(null);
  }, []);

  const handleButtonSave = useCallback(
    (buttonData) => {
      if (editingButtonIndex !== null) {
        // Editing existing button - preserve existing ID
        const newButtons = [...buttons];
        newButtons[editingButtonIndex] = {
          ...buttonData,
          id: buttons[editingButtonIndex].id,
        };
        setButtons(newButtons);
      } else {
        // Adding new button - generate unique ID
        const buttonWithId = {
          ...buttonData,
          id: uuidv4(),
        };
        setButtons((prev) => [...prev, buttonWithId]);
      }
      setIsAddingButton(false);
      setButtonBeingAdded(null);
      setEditingButtonIndex(null);
    },
    [buttons, editingButtonIndex],
  );

  const handleButtonCancel = useCallback(() => {
    setIsAddingButton(false);
    setButtonBeingAdded(null);
    setEditingButtonIndex(null);
  }, []);

  const handleButtonEdit = useCallback(
    (index) => {
      setIsAddingButton(true);
      setButtonBeingAdded(buttons[index].type);
      setEditingButtonIndex(index);
    },
    [buttons],
  );

  const handleButtonDelete = useCallback(
    (index) => {
      const newButtons = [...buttons];
      newButtons.splice(index, 1);
      setButtons(newButtons);
    },
    [buttons],
  );

  const handleButtonReorder = useCallback(
    (fromIndex, toIndex) => {
      const newButtons = [...buttons];
      const [movedButton] = newButtons.splice(fromIndex, 1);
      newButtons.splice(toIndex, 0, movedButton);
      setButtons(newButtons);
    },
    [buttons],
  );

  const isAddFlow = useMemo(
    () => isAddingButton && editingButtonIndex === null,
    [isAddingButton, editingButtonIndex],
  );

  const isEditFlow = useMemo(
    () => isAddingButton && editingButtonIndex !== null,
    [isAddingButton, editingButtonIndex],
  );

  const showAddPrimaryButton = useMemo(
    () => buttons.length === 0 && !isAddingButton,
    [buttons.length, isAddingButton],
  );

  const showAddSecondaryButton = useMemo(
    () => buttons.length === 1 && !isAddingButton,
    [buttons.length, isAddingButton],
  );

  const showDisabledSecondaryDuringPrimary = useMemo(
    () => isAddFlow && buttonBeingAdded === WEBPUSH_BUTTON_TYPES.PRIMARY && buttons.length === 0,
    [isAddFlow, buttonBeingAdded, buttons.length],
  );

  const disableSecondaryAddButton = useMemo(
    () => isAddFlow && buttonBeingAdded === WEBPUSH_BUTTON_TYPES.PRIMARY,
    [isAddFlow, buttonBeingAdded],
  );

  return {
    buttons,
    setButtons,
    isAddingButton,
    buttonBeingAdded,
    editingButtonIndex,
    handleAddPrimaryButton,
    handleAddSecondaryButton,
    handleButtonSave,
    handleButtonCancel,
    handleButtonEdit,
    handleButtonDelete,
    handleButtonReorder,
    isAddFlow,
    isEditFlow,
    showAddPrimaryButton,
    showAddSecondaryButton,
    showDisabledSecondaryDuringPrimary,
    disableSecondaryAddButton,
  };
};

