import Router from '@koa/router';
import { commentsService } from './comments.service';

const router = new Router({ prefix: '/api/comments' });

// 测试用户ID
const TEST_USER_ID = 1;

/**
 * 获取音频评论
 * GET /api/comments/:audioId
 */
router.get('/:audioId', async (ctx) => {
  try {
    const audioId = parseInt(ctx.params.audioId);
    const comments = await commentsService.getCommentsByAudioId(audioId);

    ctx.body = {
      code: 0,
      message: 'success',
      data: comments,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '获取评论失败',
    };
  }
});

/**
 * 添加评论
 * POST /api/comments
 */
router.post('/', async (ctx) => {
  try {
    const { audioId, content, rating } = ctx.request.body as {
      audioId: number;
      content: string;
      rating: number;
    };

    const comment = await commentsService.addComment(TEST_USER_ID, audioId, content, rating);

    ctx.body = {
      code: 0,
      message: '评论成功',
      data: comment,
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '评论失败',
    };
  }
});

export default router;
