import {
  Controller,
  Get,
  Patch,
  Delete,
  Param,
  Query,
  Req,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { NotificationsService } from './notifications.service';

/**
 * Notifications Controller — Kullanıcı Bildirim Endpoint'leri
 *
 * GET    /notifications              → Bildirim listesi (paginated)
 * GET    /notifications/unread-count → Okunmamış sayısı
 * PATCH  /notifications/:id/read    → Tekil okundu işaretle
 * PATCH  /notifications/read-all    → Tümünü okundu yap
 * DELETE /notifications/:id         → Silme
 */
@ApiTags('Notifications')
@ApiBearerAuth()
@Controller('notifications')
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}

  @Get()
  @ApiOperation({ summary: 'Kullanıcının bildirimlerini getir' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getNotifications(
    @Req() req: any,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    const userId = req.user?.id || req.user?.sub;
    return this.notificationsService.getUserNotifications(
      userId,
      page ? parseInt(page, 10) : 1,
      limit ? parseInt(limit, 10) : 20,
    );
  }

  @Get('unread-count')
  @ApiOperation({ summary: 'Okunmamış bildirim sayısı' })
  async getUnreadCount(@Req() req: any) {
    const userId = req.user?.id || req.user?.sub;
    return this.notificationsService.getUnreadCount(userId);
  }

  @Patch('read-all')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Tüm bildirimleri okundu olarak işaretle' })
  async markAllAsRead(@Req() req: any) {
    const userId = req.user?.id || req.user?.sub;
    return this.notificationsService.markAllAsRead(userId);
  }

  @Patch(':id/read')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Bildirimi okundu olarak işaretle' })
  async markAsRead(
    @Req() req: any,
    @Param('id') id: string,
  ) {
    const userId = req.user?.id || req.user?.sub;
    return this.notificationsService.markAsRead(id, userId);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Bildirimi sil' })
  async deleteNotification(
    @Req() req: any,
    @Param('id') id: string,
  ) {
    const userId = req.user?.id || req.user?.sub;
    return this.notificationsService.deleteNotification(id, userId);
  }
}
