import leadsData from './json/leads.json'
import activeLeads from './json/activeLeads.json'
import deadLeads from './json/deadLead.json'
import bookedLeads from './json/bookedLead.json'
import scheduledLeads from './json/scheduledLead.json'
import overdueLeads from './json/overdueLeads.json'
import visitedLeads from './json/visitedLead.json'
import tokenLeads from './json/tokenLead.json'
import leadDetail from './json/leadsDetail.json'

const mockAllLeads = (mock: any): void => {
    mock.onGet('/api/v1/leads').reply((config: any) => {
        return [200, { data: leadsData }]
    })

    mock.onGet('/api/v1/activeLeads').reply(() => {
        return [200, { data: activeLeads }]
    })

    mock.onGet('/api/v1/deadleads').reply(() => {
        return [200, { data: deadLeads }]
    })

    mock.onGet('/api/v1/bookedLeads').reply(() => {
        return [200, { data: bookedLeads }]
    })
    mock.onGet('/api/v1/tokenLeads').reply(() => {
        return [200, { data: tokenLeads }]
    })

    mock.onGet('/api/v1/scheduled').reply(() => {
        return [200, { data: scheduledLeads }]
    })

    mock.onGet('/api/v1/overdue').reply(() => {
        return [200, { data: overdueLeads }]
    })

    mock.onGet('/api/v1/visitedleads').reply(() => {
        // Replace the following with your mock data for visited leads
        return [200, { data: visitedLeads }]
    })

    mock.onPost('/api/v1/leads').reply((config: any) => {
        const data = JSON.parse(config.data)
        const newLead = {
            id: Math.floor(Math.random() * 1000),
            followUpDateTime: data.followUpDateTime,
            customerName: data.customerName,
            project: data.project,
            config: data.config,
            budgetStatus: data.budgetStatus,
            status: data.status
        }
        leadsData.push(newLead)
        return [200, { message: 'Lead added successfully', data: newLead }]
    })

    mock.onPatch('/api/v1/all-leads/{data.id}').reply((config: any) => {
        const data = JSON.parse(config.data)
        const leadId = data.id
        const updatedLeadsData = leadsData.map((lead: any) =>
            lead.id === leadId
                ? {
                      ...lead,
                      followUpDateTime: data.followUpDateTime,
                      customerName: data.customerName,
                      project: data.project,
                      config: data.config,
                      budgetStatus: data.budgetStatus,
                      status: data.status
                  }
                : lead
        )
        return [200, { message: 'Lead edited successfully', data: updatedLeadsData }]
    })

    // Mock the GET request
    mock.onGet(/^\/api\/v1\/lead\/\d+$/).reply((config: any) => {
        alert('hjb')
        const leadId = config.url.split('/').pop()

        if (!leadId) {
            throw new Error('Lead ID not found in the URL')
        } else {
            alert(leadId)
            return [200, { message: 'Lead fetched successfully', data: leadDetail }]
        }
    })

    mock.onGet(/\/api\/v1\/leads\/\d+?/).reply((config: any) => {
        const id = parseInt(config.url.split('/').pop(), 10)

        if (id) {
            return [200, { data: leadDetail }]
        } else {
            return [404, { error: 'Data not found' }]
        }
    })

    mock.onDelete(/^\/api\/v1\/leads\/\d+$/).reply((config: any) => {
        const leadId = config.url.split('/').pop()
        if (!leadId) {
            throw new Error('Lead ID not found in the URL')
        }
        // const leadsDataUpdated = leadsData.filter((lead) => lead.id !== leadId);
        return [200, { message: 'Deleted successfully' }]
    })

    // Mock the PATCH request
    mock.onPatch(/^\/api\/v1\/leads\/\d+$/).reply((config: any) => {
        const leadId = config.url.split('/').pop()
        if (!leadId) {
            throw new Error('Lead ID not found in the URL')
        }

        const data = JSON.parse(config.data)

        const updatedLeadsData = {
            id: leadId,
            name: data.name,
            status: data.status
        }
        return [200, { message: 'Lead edited successfully', data: updatedLeadsData }]
    })
}

export { mockAllLeads }
