import type { APIRoute } from 'astro';
import { autoCategorize } from '../../../lib/categories/auto-categorizer.js';

export const POST: APIRoute = async ({ request }) => {
  try {
    const body = await request.json().catch(() => ({}));
    const all = body.all === true;
    const result = await autoCategorize({ all });
    return new Response(JSON.stringify(result), {
      headers: { 'Content-Type': 'application/json' },
    });
  } catch (e: any) {
    return new Response(JSON.stringify({ error: e.message }), { status: 500 });
  }
};
