import api from './api';

export type TouchType = 'WATER' | 'FEED' | 'PRUNE' | 'TRAIN' | 'INSPECT' | 'IPM' | 'TRANSPLANT' | 'HARVEST' | 'OTHER';

export interface PlantTouchPoint {
    id: string;
    type: TouchType;
    notes?: string;
    photoUrls?: string[];
    heightCm?: number;
    widthCm?: number;
    ipmProduct?: string;
    ipmDosage?: string;
    issuesObserved?: boolean;
    issueType?: string;
    batchId: string;
    createdBy: string;
    createdAt: string;
    user?: {
        name: string;
        email: string;
    };
}

export interface IPMSchedule {
    id: string;
    batchId: string;
    product: string;
    intervalDays: number;
    lastTreatment?: string;
    nextTreatment?: string;
    isActive: boolean;
}

export interface DueTreatment {
    id: string;
    batchId: string;
    product: string;
    nextTreatment: string;
    batch: {
        name: string;
        roomId?: string;
        room?: {
            name: string;
        };
    };
}

export const touchPointsApi = {
    // Touch Points
    create: async (data: Partial<PlantTouchPoint>) => {
        const response = await api.post<PlantTouchPoint>('/touch-points', data);
        return response.data;
    },

    getAll: async () => {
        const response = await api.get<PlantTouchPoint[]>('/touch-points');
        return response.data;
    },

    getByBatch: async (batchId: string) => {
        const response = await api.get<PlantTouchPoint[]>(`/batches/${batchId}/touch-points`);
        return response.data;
    },

    // IPM
    getSchedule: async (batchId: string) => {
        const response = await api.get<IPMSchedule | { message: string }>(`/batches/${batchId}/ipm/schedule`);
        return response.data;
    },

    saveSchedule: async (data: { batchId: string; product: string; intervalDays: number }) => {
        const response = await api.post<IPMSchedule>('/ipm/schedule', data);
        return response.data;
    },

    getDueTreatments: async () => {
        const response = await api.get<DueTreatment[]>('/ipm/due');
        return response.data;
    }
};
