import {
  Controller,
  Get,
  Patch,
  Delete,
  Param,
  Query,
  HttpStatus,
  HttpException,
  ParseIntPipe,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiOperation, ApiQuery, ApiParam } from '@nestjs/swagger';
import { UserNotificationsService } from './user_notifications.service';
import { Public } from '../auth/public.decorator';

@ApiTags('userNotifications')
@Controller('userNotifications')
export class UserNotificationsController {
  constructor(
    private readonly userNotificationsService: UserNotificationsService,
  ) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get paginated list of notifications for a user' })
  @ApiQuery({ name: 'userId', required: true, type: Number })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getNotifications(
    @Query('userId', ParseIntPipe) userId: number,
    @Query('page') page: string = '1',
    @Query('limit') limit: string = '10',
  ) {
    const pageNum = parseInt(page, 10) || 1;
    const limitNum = parseInt(limit, 10) || 10;
    return this.userNotificationsService.getUserNotifications(
      userId,
      pageNum,
      limitNum,
    );
  }

  @Public()
  @Get('unread-count')
  @ApiOperation({ summary: 'Get unread notification count for a user' })
  @ApiQuery({ name: 'userId', required: true, type: Number })
  async getUnreadCount(@Query('userId', ParseIntPipe) userId: number) {
    const count = await this.userNotificationsService.getUnreadCount(userId);
    return { success: true, count };
  }

  @Public()
  @Patch('read-all')
  @ApiOperation({ summary: 'Mark all unread notifications as read for a user' })
  @ApiQuery({ name: 'userId', required: true, type: Number })
  async markAllAsRead(@Query('userId', ParseIntPipe) userId: number) {
    await this.userNotificationsService.markAllAsRead(userId);
    return { success: true, message: 'All notifications marked as read' };
  }

  @Public()
  @Patch(':id/read')
  @ApiOperation({ summary: 'Mark a single notification as read' })
  @ApiQuery({ name: 'userId', required: true, type: Number })
  async markAsRead(
    @Param('id', ParseIntPipe) id: number,
    @Query('userId', ParseIntPipe) userId: number,
  ) {
    const success = await this.userNotificationsService.markAsRead(id, userId);
    if (!success) {
      throw new HttpException('Notification not found', HttpStatus.NOT_FOUND);
    }
    return { success: true, message: 'Notification marked as read' };
  }

  @Public()
  @Delete('delete-all')
  @ApiOperation({ summary: 'Delete all notifications for a user' })
  @ApiQuery({ name: 'userId', required: true, type: Number })
  async deleteAllNotifications(@Query('userId', ParseIntPipe) userId: number) {
    await this.userNotificationsService.deleteAllNotifications(userId);
    return { success: true, message: 'All notifications deleted successfully' };
  }

  @Public()
  @Delete(':id')
  @ApiOperation({ summary: 'Delete a single notification' })
  @ApiParam({ name: 'id', required: true, type: Number })
  @ApiQuery({ name: 'userId', required: true, type: Number })
  async deleteNotification(
    @Param('id', ParseIntPipe) id: number,
    @Query('userId', ParseIntPipe) userId: number,
  ) {
    const success = await this.userNotificationsService.deleteNotification(id, userId);
    if (!success) {
      throw new HttpException('Notification not found', HttpStatus.NOT_FOUND);
    }
    return { success: true, message: 'Notification deleted successfully' };
  }
}
