import { Controller, Get, Patch, Param, Query, Req, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { NotificationService } from './notification.service';

@ApiTags('通知')
@Controller('notifications')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class NotificationController {
  constructor(private notificationService: NotificationService) {}

  @Get()
  async findAll(@Req() req: any, @Query('page') page?: string, @Query('pageSize') pageSize?: string) {
    return this.notificationService.findAll(req.user.userId, page ? parseInt(page) : 1, pageSize ? parseInt(pageSize) : 20);
  }

  @Get('unread')
  async countUnread(@Req() req: any) {
    const count = await this.notificationService.countUnread(req.user.userId);
    return { count };
  }

  @Patch(':id/read')
  async markAsRead(@Req() req: any, @Param('id') id: string) {
    await this.notificationService.markAsRead(parseInt(id), req.user.userId);
    return { ok: true };
  }

  @Patch('read-all')
  async markAllAsRead(@Req() req: any) {
    await this.notificationService.markAllAsRead(req.user.userId);
    return { ok: true };
  }
}
