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

export type GetLeadsParams = Record<string, unknown> & {
    tab_key: string | undefined;
    lead_type: string;
    old_user: number | undefined;
    launch_id: string | undefined;
    page?: number;       // New pagination parameter
    pageSize?: number;
    search?: string;
    getAllIds?: boolean;
}
interface LeadResponse {
     meta: {total: number}
    data: any;
    status: string;
    message?: string;
    total?: number;
}
export async function getExistingUsers(params?: any): Promise<any> {
    try {
        const filterParams = removeEmptyKeys(params);
        const response = await axiosInstance.get('/api/v1/de-association/user-lead', { 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,
        });
    }
}

export async function getNewUsers(params?: any): Promise<any> {
    try {
        const filterParams = removeEmptyKeys(params);
        const response = await axiosInstance.get('/api/v1/de-association/user-lead', { 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,
        });
    }
}

export async function getLaunch(params?: any): Promise<any> {
    try {
        const filterParams = removeEmptyKeys(params);
        const response = await axiosInstance.get('/api/v1/de-association/launch-list', { params: filterParams });
        if (response.status === 200) {
            return response.data;
        } else {
            throw new Error('Failed to fetch launch');
        }
    } catch (error: any) {
        notification.error({
            message: 'Error',
            description: error.message,
        });
    }
}

export async function getNewUserLaunches(params?: any): Promise<any> {
    try {
        const filterParams = removeEmptyKeys(params);
        const response = await axiosInstance.get('/api/v1/de-association/launch-list', { params: filterParams });
        if (response.status === 200) {
            return response.data;
        } else {
            throw new Error('Failed to fetch new user launches');
        }
    } catch (error: any) {
        notification.error({
            message: 'Error',
            description: error.message,
        });
    }
}

export const updateLeadTransfer = async(data: {
    launch_id: number | string
    transfer_launch_id?: number
    old_user: number
    new_user: number
    role_id: number
    city_id: number
    leads_id: React.Key[],
    lead_type: string
}) => {
    try {
        const response = await axiosInstance.patch('/api/v1/de-association/lead-transfer-new-user', data)
        if (response.status === 200) {
            return response.data
        } else {
            throw new Error('Failed to update lead transfer')
        }
    } catch (error) {
        console.error('Error updating lead transfer:', error)
        throw error
    }
}

export async function getLeads(params?: GetLeadsParams): Promise<LeadResponse | undefined> {
    try {
        // Provide a fallback if params is undefined
        const filterParams = removeEmptyKeys(params || {});

        const response = await axiosInstance.get<LeadResponse>('/api/v1/de-association/get-leads', {
            params: filterParams,
        });

        if (response.status === 200) {
            return response.data;
        } else {
            throw new Error('Failed to fetch leads');
        }
    } catch (error: unknown) {
        if (error instanceof Error) {
            notification.error({
                message: 'Error',
                description: error.message,
            });
        }
    }
}