import { api } from '@/lib/api'
import type { About, PaginatedResponse } from '@/types/api'

export const aboutService = {
    // List all about entries (assuming pagination like others)
    getAll: async (page = 1, limit = 10) => {
        const response = await api.get<PaginatedResponse<About>>('/v1/admin/about', {
            params: { page, limit }
        })
        return response.data
    },

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

    // Create new about entry
    create: async (data: any) => {
        const formData = new FormData()

        const fields = [
            'title', 'image_sub', 'birthday', 'city', 'study', 'website',
            'phone', 'age', 'interests', 'degree', 'x', 'mail',
            'done', 'project_done', 'user_h', 'hapy_user',
            'great', 'great_reviews', 'team', 'support_team',
            'is_active', 'counter_active'
        ]

        fields.forEach(field => {
            if (data[field] !== undefined && data[field] !== null) {
                formData.append(field, String(data[field]))
            }
        })

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

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

    // Update existing about entry
    update: async (id: string, data: any) => {
        const formData = new FormData()

        const fields = [
            'title', 'image_sub', 'birthday', 'city', 'study', 'website',
            'phone', 'age', 'interests', 'degree', 'x', 'mail',
            'done', 'project_done', 'user_h', 'hapy_user',
            'great', 'great_reviews', 'team', 'support_team',
            'is_active', 'counter_active'
        ]

        fields.forEach(field => {
            if (data[field] !== undefined && data[field] !== null) {
                formData.append(field, String(data[field]))
            }
        })

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

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

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