import Router from '@koa/router';
import { searchService } from './search.service';

const router = new Router();

/**
 * 搜索音频
 * GET /api/search?q=关键词
 */
router.get('/', async (ctx) => {
  try {
    const { q, limit } = ctx.query as { q?: string; limit?: string };

    if (!q || q.trim() === '') {
      ctx.body = {
        code: 0,
        message: 'success',
        data: [],
      };
      return;
    }

    const results = await searchService.searchAudio(q, parseInt(limit || '20'));

    ctx.body = {
      code: 0,
      message: 'success',
      data: results,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '搜索失败',
    };
  }
});

export default router;
