import { Hono } from 'hono';
import { z } from 'zod';

import { getDb } from '@/shared/db';
import { classifyHandle, reclassifyDevice } from '@/features/prospect';

const bodySchema = z.object({
  deviceId: z.string().uuid(),
  handle:   z.string().min(1).optional(),
});

export const prospectsAdminRouter = new Hono()
  .post('/reclassify', async (c) => {
    const pool = getDb();
    const body = bodySchema.parse(await c.req.json());
    if (body.handle) {
      const prospect = await classifyHandle(pool, body.deviceId, body.handle);
      return c.json({ classified: 1, prospect });
    }
    const count = await reclassifyDevice(pool, body.deviceId);
    return c.json({ classified: count });
  });
