import { apiClient } from './client';
import { Product, CreateProductData, UpdateProductData, ApiResponse } from '@/types/product';

export const productsApi = {
    // Get all products for a package
    getProducts: async (packageName: string, token: string): Promise<Product[]> => {
        return apiClient.get<Product[]>(`/package-names/${packageName}/products`, token);
    },

    // Get single product
    getProduct: async (packageName: string, productId: number, token: string): Promise<Product> => {
        return apiClient.get<Product>(`/package-names/${packageName}/products/${productId}`, token);
    },

    // Create new product
    createProduct: async (packageName: string, data: CreateProductData, token: string): Promise<ApiResponse<Product>> => {
        const formData = new FormData();

        // Append all fields to FormData
        Object.entries(data).forEach(([key, value]) => {
            if (value !== null && value !== undefined) {
                if (key === 'descriptions' && Array.isArray(value)) {
                    // Handle descriptions array
                    value.forEach((desc, index) => {
                        formData.append(`descriptions[${index}]`, desc);
                    });
                } else {
                    formData.append(key, String(value));
                }
            }
        });

        return apiClient.post<ApiResponse<Product>>(`/package-names/${packageName}/products`, formData, token);
    },

    // Update product (using PATCH)
    updateProduct: async (packageName: string, productId: number, data: UpdateProductData, token: string): Promise<ApiResponse<Product>> => {
        return apiClient.patch<ApiResponse<Product>>(
            `/package-names/${packageName}/products/${productId}`,
            data,
            token
        );
    },

    // Delete product
    deleteProduct: async (packageName: string, productId: number, token: string): Promise<ApiResponse<any>> => {
        return apiClient.delete<ApiResponse<any>>(`/package-names/${packageName}/products/${productId}`, undefined, token);
    },

    // Alternative update method using POST with _method spoofing (if PATCH is not supported)
    updateProductWithSpoof: async (packageName: string, productId: number, data: UpdateProductData, token: string): Promise<ApiResponse<Product>> => {
        const formData = new FormData();
        formData.append('_method', 'PATCH');

        Object.entries(data).forEach(([key, value]) => {
            if (value !== null && value !== undefined) {
                if (key === 'descriptions' && Array.isArray(value)) {
                    value.forEach((desc, index) => {
                        formData.append(`descriptions[${index}]`, desc);
                    });
                } else {
                    formData.append(key, String(value));
                }
            }
        });

        return apiClient.post<ApiResponse<Product>>(`/package-names/${packageName}/products/${productId}`, formData, token);
    }
};