import axiosInstance from '@/utills/axiosInstance';
import { removeEmptyKeys } from '@/utills/helper';
import { notification } from 'antd';

// Function to get users based on role_id and city_id for Existing User
export async function getExistingUsers(params?: any): Promise<any> {
  try {
    const filterParams = removeEmptyKeys(params);
    const response = await axiosInstance.get('/api/v1/de-association', { params: filterParams });
    if (response.status === 200) {
      return response.data;
    } else {
      throw new Error('Failed to fetch existing users');
    }
  } catch (error: any) {
    notification.error({
      message: 'Error',
      description: error.message || 'An unexpected error occurred',
    });
  }
}
// Function to get users based on role_id, city_id, and existing_user_id for New User
export async function getNewUsers(params?: any): Promise<any> {
  try {
    const filterParams = removeEmptyKeys(params);
    const response = await axiosInstance.get('/api/v1/de-association', { params: filterParams });
    if (response.status === 200) {
      return response.data;
    } else {
      throw new Error('Failed to fetch new users');
    }
  } catch (error: any) {
    notification.error({
      message: 'Error',
      description: error.message || 'An unexpected error occurred',
    });
  }
}

export const updateDeassociation = async(data: {
    old_user: number
    new_user: number
    role_id: number
    city_id: number
}) => {
  try {
    const response = await axiosInstance.patch('/api/v1/de-association', data);
    if (response.status === 200) {
      return response.data;
    } else {
      throw new Error('Failed to update Deassociation');
    }
  } catch (error) {
    console.error('Error updating lead transfer:', error);
    throw error;
  }
};
