import api from './api';

export interface IPMSchedule {
    id: string;
    batchId: string;
    product: string;
    intervalDays: number;
    lastTreatment?: string; // ISO date
    nextTreatment?: string; // ISO date
    isActive: boolean;
    createdAt: string;
    updatedAt: string;
}

export interface UpsertIPMScheduleData {
    product: string;
    intervalDays: number;
    lastTreatment?: string | null; // ISO date YYYY-MM-DD
    nextTreatment?: string | null; // ISO date YYYY-MM-DD
    isActive?: boolean;
}

export const ipmSchedulesApi = {
    get: async (batchId: string) => {
        try {
            const response = await api.get<IPMSchedule>(`/ipm/schedules/${batchId}`);
            return response.data;
        } catch (error: any) {
            if (error.response?.status === 404) {
                return null;
            }
            throw error;
        }
    },

    upsert: async (batchId: string, data: UpsertIPMScheduleData) => {
        const response = await api.post<IPMSchedule>(`/ipm/schedules/${batchId}`, data);
        return response.data;
    }
};
