import { Controller, Get, Param, Patch, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { SuperAdminPermissions } from '../../common/decorators/superadmin-permissions.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { SuperAdminJwtAuthGuard } from '../../common/guards/super-admin-jwt-auth.guard';
import { SuperAdminPermissionsGuard } from '../../common/guards/superadmin-permissions.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { SUPERADMIN_PERMISSIONS } from '../superadmin/superadmin-permissions';
import {
  NotificationQueryDto,
  NotificationUnreadCountQueryDto,
} from './dto/notification-query.dto';
import { NotificationsService } from './notifications.service';

@ApiTags('Notifications')
@ApiBearerAuth()
@Controller('notifications')
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}

  @ApiBearerAuth()
  @UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.NOTIFICATIONS_READ)
  @Get('superadmin')
  async getForSuperAdmin(@Query() query: NotificationQueryDto) {
    return this.notificationsService.getForSuperAdmin(query);
  }

  @UseGuards(JwtAuthGuard)
  @Get()
  async getMine(@CurrentUser() user: JwtPayload, @Query() query: NotificationQueryDto) {
    return this.notificationsService.getMine(user.sub, query);
  }

  @UseGuards(JwtAuthGuard)
  @Get('unread-count')
  async getUnreadCount(
    @CurrentUser() user: JwtPayload,
    @Query() query: NotificationUnreadCountQueryDto,
  ) {
    return this.notificationsService.getUnreadCountByCategory(user.sub, query.category);
  }

  @UseGuards(JwtAuthGuard)
  @Get('unread-counts')
  async getUnreadCounts(@CurrentUser() user: JwtPayload) {
    return this.notificationsService.getUnreadCounts(user.sub);
  }

  @UseGuards(JwtAuthGuard)
  @Patch('read-all')
  async markAllRead(@CurrentUser() user: JwtPayload) {
    return this.notificationsService.markAllRead(user.sub);
  }

  @UseGuards(JwtAuthGuard)
  @Patch(':id/read')
  async markRead(@CurrentUser() user: JwtPayload, @Param('id') notificationId: string) {
    return this.notificationsService.markRead(user.sub, notificationId);
  }
}
