import { apiClient } from './client';
import { Reminder, CreateReminderData, UpdateReminderData, ApiResponse } from '@/types/reminder';

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

    // Get single reminder
    getReminder: async (packageName: string, reminderId: number, token: string): Promise<Reminder> => {
        return apiClient.get<Reminder>(`/package-names/${packageName}/reminders/${reminderId}`, token);
    },

    // Create new reminder
    createReminder: async (packageName: string, data: CreateReminderData, token: string): Promise<ApiResponse<Reminder>> => {
        const formData = new FormData();
        
        // Append all fields to FormData
        Object.entries(data).forEach(([key, value]) => {
            if (value !== null && value !== undefined) {
                if (key === 'data' && typeof value === 'object') {
                    formData.append(key, JSON.stringify(value));
                } else if (typeof value === 'boolean') {
                    // Laravel's `boolean` rule rejects the strings "true"/"false"; send 1/0
                    formData.append(key, value ? '1' : '0');
                } else {
                    formData.append(key, String(value));
                }
            }
        });

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

    // Update reminder (using PUT)
    updateReminder: async (packageName: string, reminderId: number, data: UpdateReminderData, token: string): Promise<ApiResponse<Reminder>> => {
        return apiClient.put<ApiResponse<Reminder>>(
            `/package-names/${packageName}/reminders/${reminderId}`,
            data,
            token
        );
    },

    // Delete reminder
    deleteReminder: async (packageName: string, reminderId: number, token: string): Promise<ApiResponse<any>> => {
        return apiClient.delete<ApiResponse<any>>(`/package-names/${packageName}/reminders/${reminderId}`, undefined, token);
    },

    // Alternative update method using POST with _method spoofing (if PUT is not supported)
    updateReminderWithSpoof: async (packageName: string, reminderId: number, data: UpdateReminderData, token: string): Promise<ApiResponse<Reminder>> => {
        const formData = new FormData();
        formData.append('_method', 'PUT');
        
        Object.entries(data).forEach(([key, value]) => {
            if (value !== null && value !== undefined) {
                if (key === 'data' && typeof value === 'object') {
                    formData.append(key, JSON.stringify(value));
                } else if (typeof value === 'boolean') {
                    // Laravel's `boolean` rule rejects the strings "true"/"false"; send 1/0
                    formData.append(key, value ? '1' : '0');
                } else {
                    formData.append(key, String(value));
                }
            }
        });

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