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

import { getDb } from '@/shared/db';
import { getProspect, listProspects } from '@/features/prospect';

const tierParam  = z.enum(['BLOCK', 'PEER', 'PROSPECT', 'REGULAR', 'VIP']).optional();
const heatParam  = z.enum(['HOT', 'WARM', 'COLD', 'DEAD']).optional();
const limitParam = z.coerce.number().int().positive().max(500).default(100);
const handleParam = z.string().min(1);
const deviceIdParam = z.string().uuid();

export const prospectsMyRouter = new Hono()
  .get('/', async (c) => {
    const pool      = getDb();
    const tier      = tierParam.parse(c.req.query('tier') ?? undefined);
    const heat      = heatParam.parse(c.req.query('heat') ?? undefined);
    const metro     = c.req.query('metro') ?? undefined;
    const limit     = limitParam.parse(c.req.query('limit') ?? '100');
    const deviceId  = c.req.query('deviceId') ?? undefined;
    return c.json(await listProspects(pool, { tier, heat, metro, deviceId, limit }));
  })
  .get('/:handle', async (c) => {
    const pool     = getDb();
    const handle   = handleParam.parse(c.req.param('handle'));
    const deviceId = deviceIdParam.parse(c.req.query('deviceId') ?? '');
    const prospect = await getProspect(pool, deviceId, handle);
    if (!prospect) return c.json({ error: 'not_found' }, 404);
    return c.json(prospect);
  });
