import {
  Controller,
  Get,
  Put,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { NotificationService } from './notification.service';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

// 学校端通知控制器
@Controller('school/notifications')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('school')
export class SchoolNotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @Get()
  getNotifications(@Request() req: any, @Query() query: any) {
    return this.notificationService.getNotifications(
      req.user.tenantId,
      'SCHOOL',
      req.user.userId,
      query
    );
  }

  @Get('unread-count')
  getUnreadCount(@Request() req: any) {
    return this.notificationService.getUnreadCount(
      req.user.tenantId,
      'SCHOOL',
      req.user.userId
    );
  }

  @Put(':id/read')
  markAsRead(@Request() req: any, @Param('id') id: string) {
    return this.notificationService.markAsRead(
      req.user.tenantId,
      +id,
      'SCHOOL',
      req.user.userId
    );
  }

  @Put('read-all')
  markAllAsRead(@Request() req: any) {
    return this.notificationService.markAllAsRead(
      req.user.tenantId,
      'SCHOOL',
      req.user.userId
    );
  }
}

// 教师端通知控制器
@Controller('teacher/notifications')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('teacher')
export class TeacherNotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @Get()
  getNotifications(@Request() req: any, @Query() query: any) {
    return this.notificationService.getNotifications(
      req.user.tenantId,
      'TEACHER',
      req.user.userId,
      query
    );
  }

  @Get('unread-count')
  getUnreadCount(@Request() req: any) {
    return this.notificationService.getUnreadCount(
      req.user.tenantId,
      'TEACHER',
      req.user.userId
    );
  }

  @Put(':id/read')
  markAsRead(@Request() req: any, @Param('id') id: string) {
    return this.notificationService.markAsRead(
      req.user.tenantId,
      +id,
      'TEACHER',
      req.user.userId
    );
  }

  @Put('read-all')
  markAllAsRead(@Request() req: any) {
    return this.notificationService.markAllAsRead(
      req.user.tenantId,
      'TEACHER',
      req.user.userId
    );
  }
}

// 家长端通知控制器
@Controller('parent/notifications')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('parent')
export class ParentNotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  @Get()
  getNotifications(@Request() req: any, @Query() query: any) {
    return this.notificationService.getNotifications(
      req.user.tenantId,
      'PARENT',
      req.user.userId,
      query
    );
  }

  @Get('unread-count')
  getUnreadCount(@Request() req: any) {
    return this.notificationService.getUnreadCount(
      req.user.tenantId,
      'PARENT',
      req.user.userId
    );
  }

  @Put(':id/read')
  markAsRead(@Request() req: any, @Param('id') id: string) {
    return this.notificationService.markAsRead(
      req.user.tenantId,
      +id,
      'PARENT',
      req.user.userId
    );
  }

  @Put('read-all')
  markAllAsRead(@Request() req: any) {
    return this.notificationService.markAllAsRead(
      req.user.tenantId,
      'PARENT',
      req.user.userId
    );
  }
}
