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

import { unifiedSearch } from '@/features/search';

const queryParam = z.string().trim().min(1).max(256);
const limitParam = z.coerce.number().int().positive().max(100).default(30);
const deviceIdParam = z.string().min(1).optional();

export const searchMyRouter = new Hono().get('/', async (c) => {
  const qParsed = queryParam.safeParse(c.req.query('q') ?? '');
  if (!qParsed.success) {
    return c.json({ error: 'query parameter `q` is required (1–256 chars after trim)' }, 400);
  }
  const query = qParsed.data;
  const limit = limitParam.parse(c.req.query('limit') ?? '30');
  const deviceId = deviceIdParam.parse(c.req.query('deviceId'));
  return c.json(await unifiedSearch(query, { deviceId, limit }));
});
