import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { createOrUpdateSchedule, getSchedule, getDueTreatments } from '../controllers/ipm.controller';

export async function ipmRoutes(fastify: FastifyInstance) {
    const authenticate = async (request: FastifyRequest, reply: FastifyReply) => {
        try {
            await request.jwtVerify();
        } catch (err) {
            reply.send(err);
        }
    };

    fastify.post('/api/ipm/schedule', { preHandler: [authenticate] }, createOrUpdateSchedule);

    interface GetScheduleRequest {
        Params: {
            batchId: string;
        }
    }
    fastify.get<GetScheduleRequest>('/api/batches/:batchId/ipm/schedule', { preHandler: [authenticate] }, getSchedule);
    fastify.get('/api/ipm/due', { preHandler: [authenticate] }, getDueTreatments);
}
