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

export const serviceService = {
    // List all services
    getAll: async () => {
        const response = await api.get<Service[]>('/v1/admin/services')
        return response.data
    },

    // Get single service by ID
    getById: async (id: string) => {
        const response = await api.get<Service>(`/v1/admin/services/${id}`)
        return response.data
    },

    // Create new service
    create: async (data: any) => {
        const formData = new FormData()
        formData.append('title', data.title)
        if (data.content) formData.append('content', data.content)
        if (data.is_active !== undefined) formData.append('is_active', String(data.is_active))

        if (data.image instanceof File) {
            formData.append('image', data.image)
        }

        const response = await api.post<Service>('/v1/admin/services', formData, {
            headers: { 'Content-Type': 'multipart/form-data' },
        })
        return response.data
    },

    // Update existing service
    update: async (id: string, data: any) => {
        const formData = new FormData()
        if (data.title) formData.append('title', data.title)
        if (data.content) formData.append('content', data.content)
        if (data.is_active !== undefined) formData.append('is_active', String(data.is_active))
        if (data.slug) formData.append('slug', data.slug)

        if (data.image instanceof File) {
            formData.append('image', data.image)
        }

        const response = await api.put<Service>(`/v1/admin/services/${id}`, formData, {
            headers: { 'Content-Type': 'multipart/form-data' },
        })
        return response.data
    },

    // Delete service
    delete: async (id: string) => {
        await api.delete(`/v1/admin/services/${id}`)
    }
}
