// src/routes/api/crop-progress/[id]/+server.ts
import { type RequestHandler } from '@sveltejs/kit';
import { activeProcesses, processInfo } from '$lib/server/processes';

export const DELETE: RequestHandler = async ({ params }) => {
    const { id } = params;

    const proc = activeProcesses.get(id);
    if (!proc) {
        return new Response(JSON.stringify({ error: 'Process not found or already completed' }), {
            status: 404,
            headers: { 'Content-Type': 'application/json' }
        });
    }

    try {
        proc.kill('SIGKILL');
        activeProcesses.delete(id);
        const info = processInfo.get(id);
        if (info) {
            info.status = 'cancelled';
            info.message = 'Process cancelled by user';
        }

        return new Response(JSON.stringify({ success: true, id }), {
            headers: { 'Content-Type': 'application/json' }
        });
    } catch (err) {
        return new Response(JSON.stringify({ error: (err as Error).message }), {
            status: 500,
            headers: { 'Content-Type': 'application/json' }
        });
    }
};
