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 || '搜索失败',
    };
  }
});

/**
 * 获取热门搜索词
 * GET /api/search/hot
 */
router.get('/hot', async (ctx) => {
  try {
    const { limit } = ctx.query as { limit?: string };
    const hotSearches = await searchService.getHotSearches(parseInt(limit || '10'));

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

/**
 * 获取搜索历史
 * GET /api/search/history?userId=1
 */
router.get('/history', async (ctx) => {
  try {
    const { userId, limit } = ctx.query as { userId?: string; limit?: string };
    const userIdNum = parseInt(userId || '0') || 0;

    const history = await searchService.getSearchHistory(userIdNum, parseInt(limit || '5'));

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

/**
 * 保存搜索历史
 * POST /api/search/history
 */
router.post('/history', async (ctx) => {
  try {
    const { userId, keyword } = ctx.request.body as { userId?: number; keyword?: string };

    if (!keyword || keyword.trim() === '') {
      ctx.body = {
        code: 400,
        message: '关键词不能为空',
      };
      return;
    }

    const userIdNum = userId || 0;
    await searchService.saveSearchHistory(userIdNum, keyword);

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

/**
 * 删除单条搜索历史
 * DELETE /api/search/history?userId=1&keyword=关键词
 */
router.delete('/history', async (ctx) => {
  try {
    const { userId, keyword } = ctx.query as { userId?: string; keyword?: string };

    if (!keyword) {
      ctx.body = {
        code: 400,
        message: '关键词不能为空',
      };
      return;
    }

    const userIdNum = parseInt(userId || '0') || 0;
    await searchService.deleteSearchHistoryItem(userIdNum, keyword);

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

/**
 * 清空搜索历史
 * DELETE /api/search/history/all?userId=1
 */
router.delete('/history/all', async (ctx) => {
  try {
    const { userId } = ctx.query as { userId?: string };
    const userIdNum = parseInt(userId || '0') || 0;

    await searchService.clearSearchHistory(userIdNum);

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

export default router;
