import Router from '@koa/router';
import { categoriesService } from './categories.service';

const router = new Router();

/**
 * 获取分类列表
 * GET /api/categories
 */
router.get('/', async (ctx) => {
  try {
    const categories = await categoriesService.getCategories();
    ctx.body = {
      code: 0,
      message: 'success',
      data: categories,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取分类失败',
    };
  }
});

/**
 * 按分类获取音频
 * GET /api/categories/:id
 */
router.get('/:id', async (ctx) => {
  try {
    const { id } = ctx.params;
    const { page, pageSize } = ctx.query as { page?: string; pageSize?: string };

    const result = await categoriesService.getAudiosByCategory(
      parseInt(id),
      parseInt(page || '1'),
      parseInt(pageSize || '20')
    );

    ctx.body = {
      code: 0,
      message: 'success',
      data: result,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取分类音频失败',
    };
  }
});

export default router;
