import { apiClient } from './client';
import { PackageName, CreatePackageData, UpdatePackageData, ApiResponse } from '@/types/package';

export const packagesApi = {
    // Get all package names
    getAllPackages: async (token: string): Promise<PackageName[]> => {
        return apiClient.get<PackageName[]>('/package-names', token);
    },

    // Get single package by name
    getPackageByName: async (name: string, token: string): Promise<PackageName> => {
        return apiClient.get<PackageName>(`/package-names/${name}`, token);
    },

    // Create new package
    createPackage: async (data: CreatePackageData, token: string): Promise<ApiResponse<PackageName>> => {
        const formData = new FormData();

        // Append all fields to FormData
        Object.entries(data).forEach(([key, value]) => {
            if (value !== null && value !== undefined) {
                if (key === 'avatar' && value instanceof File) {
                    formData.append('avatar', value);
                } else if (key === 'firebase_json' && value instanceof File) {
                    formData.append('firebase_json', value);
                } else {
                    formData.append(key, String(value));
                }
            }
        });

        return apiClient.post<ApiResponse<PackageName>>('/package-names', formData, token);
    },

    // Update package
    updatePackage: async (name: string, data: UpdatePackageData, token: string): Promise<ApiResponse<PackageName>> => {
        const formData = new FormData();

        // Add method spoofing for Laravel

        // Append all fields to FormData
        Object.entries(data).forEach(([key, value]) => {
            if (value !== null && value !== undefined) {
                if (key === 'avatar' && value instanceof File) {
                    formData.append('avatar', value);
                } else if (key === 'firebase_json' && value instanceof File) {
                    formData.append('firebase_json', value);
                } else {
                    formData.append(key, String(value));
                }
            }
        });

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

    // Delete package
    deletePackage: async (name: string, token: string): Promise<ApiResponse<any>> => {
        return apiClient.delete<ApiResponse<any>>(`/package-names/${name}`, undefined, token);
    },

    // Upload avatar separately if needed
    uploadAvatar: async (name: string, avatarFile: File, token: string): Promise<ApiResponse<any>> => {
        const formData = new FormData();
        formData.append('avatar', avatarFile);

        return apiClient.post<ApiResponse<any>>(`/package-names/${name}/avatar`, formData, token);
    },

    // Upload firebase config separately if needed
    uploadFirebaseConfig: async (name: string, firebaseFile: File, token: string): Promise<ApiResponse<any>> => {
        const formData = new FormData();
        formData.append('firebase_json', firebaseFile);

        return apiClient.post<ApiResponse<any>>(`/package-names/${name}/firebase`, formData, token);
    }
};