import { CampaignRequest, CampaignRuleRequest } from '@/types/services/campaign'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { api } from '../api'

export const useCampaignsMutation = () => {
  const queryClient = useQueryClient()

  const createCampaign = useMutation({
    mutationFn: async (newCampaign: CampaignRequest) => {
      const response = await api.post('/marketing/campaigns', newCampaign)
      return response.data
    },
    onSuccess: () => {
      toast.success('Campaign created successfully!')
      queryClient.invalidateQueries({ queryKey: ['campaigns'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
    }
  })

  const updateCampaign = useMutation({
    mutationFn: async ({ id, payload }: { id: string; payload: CampaignRequest }) => {
      const response = await api.put(`/marketing/campaigns/${id}`, payload)
      return response.data
    },
    onSuccess: () => {
      toast.success('Campaign updated successfully!')
      queryClient.invalidateQueries({ queryKey: ['campaigns'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
    }
  })

  const deleteCampaign = useMutation({
    mutationFn: async (id: string) => {
      const response = await api.delete(`/marketing/campaigns/${id}`)
      return response.data
    },
    onSuccess: () => {
      toast.success('Campaign deleted successfully!')
      queryClient.invalidateQueries({ queryKey: ['campaigns'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
    }
  })

  return { createCampaign, updateCampaign, deleteCampaign }
}

export const useCampaignRulesMutation = () => {
  const queryClient = useQueryClient()

  const createCampaignRule = useMutation({
    mutationFn: async (newCampaignRule: CampaignRuleRequest) => {
      const response = await api.post('/marketing/campaign-rules', newCampaignRule)
      return response.data
    },
    onSuccess: () => {
      toast.success('CampaignRule created successfully!')
      queryClient.invalidateQueries({ queryKey: ['campaign-rules/campaign'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
    }
  })

  const updateCampaignRule = useMutation({
    mutationFn: async ({ id, payload }: { id: string; payload: CampaignRuleRequest }) => {
      const response = await api.put(`/marketing/campaign-rules/${id}`, payload)
      return response.data
    },
    onSuccess: () => {
      toast.success('CampaignRule updated successfully!')
      queryClient.invalidateQueries({ queryKey: ['campaign-rules/campaign'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
    }
  })

  const deleteCampaignRule = useMutation({
    mutationFn: async (id: string) => {
      const response = await api.delete(`/marketing/campaign-rules/${id}`)
      return response.data
    },
    onSuccess: () => {
      toast.success('CampaignRule deleted successfully!')
      queryClient.invalidateQueries({ queryKey: ['campaign-rules/campaign'] })
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
    }
  })

  return { createCampaignRule, updateCampaignRule, deleteCampaignRule }
}
