import { api } from '@/lib/api'
import type { Contact } from '@/types/api'

export const contactService = {
    getAll: async (page = 1, limit = 20) => {
        // Assuming backend returns PaginatedResponse or array. 
        // Adapting to array for consistency with previous modules if needed, 
        // but usually contacts are paginated.
        const response = await api.get<Contact[] | any>(`/v1/admin/contacts?page=${page}&limit=${limit}`)
        return response.data
    },

    getById: async (id: string) => {
        const response = await api.get<Contact>(`/v1/admin/contacts/${id}`)
        return response.data
    },

    create: async (data: Partial<Contact> & { tag_ids?: string[] }) => {
        const response = await api.post<Contact>('/v1/admin/contacts', data)
        return response.data
    },

    update: async (id: string, data: Partial<Contact> & { tag_ids?: string[] }) => {
        const response = await api.put<Contact>(`/v1/admin/contacts/${id}`, data)
        return response.data
    },

    delete: async (id: string) => {
        await api.delete(`/v1/admin/contacts/${id}`)
    },

    importCsv: async (file: File) => {
        const formData = new FormData()
        formData.append('file', file)
        const response = await api.post('/v1/admin/contacts/import', formData, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
        })
        return response.data
    },

    exportCsv: async () => {
        const response = await api.get('/v1/admin/contacts/export', {
            responseType: 'blob',
        })
        return response.data
    }
}
