import Router from '@koa/router';
import { notificationsService } from './notifications.service';

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

// 测试用户ID
const TEST_USER_ID = 'test-user';

/**
 * 获取通知列表
 * GET /api/notifications
 */
router.get('/', async (ctx) => {
  try {
    const notifications = await notificationsService.getNotifications(TEST_USER_ID);

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

/**
 * 标记通知为已读
 * POST /api/notifications/read
 */
router.post('/read', async (ctx) => {
  try {
    const { id } = ctx.request.body as { id: string };
    await notificationsService.markAsRead(id);

    ctx.body = {
      code: 0,
      message: 'success',
    };
  } catch (error: any) {
    ctx.body = {
      code: 400,
      message: error.message || '标记失败',
    };
  }
});

export default router;
