import apiService from '../../src/APIRequestService/APIServices';

export const fetchMenuItems = async () => {
  console.log("applying fetch menu items");
  try {
    const response = await apiService.get('/api1/submenu1');
    console.log("response", response);

    if (response.status !== 200) {
      throw new Error(`Error: ${response.status}`);
    }

    if (!response.data || !Array.isArray(response.data)) {
      throw new Error('Unexpected data format, expected an array');
    }

    console.log('Fetched Menu Items:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error fetching menu items:', error);
    throw error;
  }
};

export const addMenuItem = async (formData) => {
  try {
    const payload = {
      menuItemDesc: formData.menuItemDesc,
      menuId: 0,
      itemSeq: formData.itemSeq || 0,
      moduleName: formData.moduleName || "",
      main_menu_action_name: formData.main_menu_action_name || "#",
      main_menu_icon_name: formData.main_menu_icon_name || "fa-circle",
      status: formData.status === "true"
    };
    
    console.log("Final payload:", payload);
    const response = await apiService.post('/api1/Sec_menuDet/', payload);
    return response.data;
  } catch (error) {
    console.error('Error adding menu item:', error);
    throw error;
  }
};

export const updateMenuItem = async (id, formData) => {
  try {
    const payload = {
      menuItemId: Number(id),
      menuItemDesc: formData.menuItemDesc,
      menuId: formData.menuId || 0,
      itemSeq: formData.itemSeq || 0,
      moduleName: formData.moduleName || "",
      main_menu_action_name: formData.main_menu_action_name || "#",
      main_menu_icon_name: formData.main_menu_icon_name || "fa-circle",
      status: formData.status === "Enable" || formData.status === true
    };

    console.log("Updating menu item #"+id, payload);
    const response = await apiService.post('/api1/Sec_menuDet/', payload);
    console.log("Update successful:", response.data);
    return response.data;
  } catch (error) {
    console.error('Update failed:', {
      id: id,
      error: error.response?.data || error.message
    });
    throw error;
  }
};

export const deleteMenuItem = async (id) => {
  try {
    const response = await apiService.delete(`/api1/menu/${id}`);
    return response.data;
  } catch (error) {
    throw error;
  }
}

export const getSubmenuItems = async (id) => {
  try {
    const response = await apiService.get(`/api1/submenu1/${id}`);
    console.log("get submenu response: ", response.data);
    return response.data;
  } catch (error) {
    throw error;
  }
}

export const addSubmenuItem = async (parentMenuId, formData) => {
  try {
    const payload = {
      menuItemDesc: formData.menuItemDesc,
      menuId: Number(parentMenuId),
      itemSeq: formData.itemSeq || 0,
      moduleName: formData.moduleName || "",
      main_menu_action_name: formData.main_menu_action_name || "#",
      main_menu_icon_name: formData.main_menu_icon_name || "fa-circle",
      status: formData.status === "Enable"
    };

    console.log("Adding submenu with payload:", payload);
    const response = await apiService.post('/api1/Sec_menuDet/', payload);
    return response.data;
  } catch (error) {
    console.error('Error adding submenu:', error);
    throw error;
  }
};

export const updateSubmenuItem = async (submenuId, formData) => {
  const payload = {
    menuItemId: Number(submenuId),
    menuItemDesc: formData.menuItemDesc,
    menuId: Number(formData.menuId),
    itemSeq: formData.itemSeq || 0,
    moduleName: formData.moduleName || "",
    main_menu_action_name: formData.main_menu_action_name || "#",
    main_menu_icon_name: formData.main_menu_icon_name || "fa-circle",
    status: formData.status === "Enable" || formData.status === true
  };
  
  try {
    console.log("Updating submenu with payload:", payload);
    const response = await apiService.post('/api1/Sec_menuDet/', payload);
    return response.data;
  } catch (error) {
    console.error('Error updating submenu:', {
      error: error.response?.data || error.message,
      payload: payload
    });
    throw error;
  }
};

export const deleteSubmenuItem = async (submenuId) => {
  try {
    console.log("Deleting submenu with ID:", submenuId);
    const response = await apiService.post('/api1/Sec_menuDet/', {
      menuItemId: Number(submenuId)
    });
    console.log("Delete successful:", response.data);
    return response.data;
  } catch (error) {
    console.error('Error deleting submenu:', {
      status: error.response?.status,
      error: error.response?.data || error.message,
      submenuId: submenuId
    });
    throw error;
  }
};