import {
  Controller,
  Post,
  Get,
  Body,
  Headers,
  Query,
  ValidationPipe,
} from "@nestjs/common"
import { NotificationService } from "./notification.service"
import { SendNotificationDto } from "./dto/send-notification.dto"
import { SaveFcmTokenDto } from "./dto/save-fcm-token.dto"
import { MarkReadDto } from "./dto/mark-read.dto"

@Controller("notifications")
export class NotificationController {
  constructor(private readonly notificationService: NotificationService) {}

  /**
   * Save or update FCM token
   * POST /notifications/save-token
   */
  @Post("save-token")
  async saveFcmToken(
    @Body(ValidationPipe) dto: SaveFcmTokenDto,
    @Headers("authorization") authorization: string,
  ) {
    const token = authorization?.replace("Bearer ", "")
    return this.notificationService.saveFcmToken(dto, token)
  }

  /**
   * Send push notification
   * POST /notifications/send
   */
  @Post("send")
  async sendNotification(@Body(ValidationPipe) dto: SendNotificationDto) {
    return this.notificationService.sendNotification(dto)
  }

  /**
   * Mark notification as read
   * POST /notifications/mark-read
   */
  @Post("mark-read")
  async markNotificationAsRead(
    @Body(ValidationPipe) dto: MarkReadDto,
    @Headers("authorization") authorization: string,
  ) {
    const token = authorization?.replace("Bearer ", "")
    return this.notificationService.markNotificationAsRead(dto, token)
  }

  /**
   * List notifications for authenticated user
   * GET /notifications/list
   */
  @Get("list")
  async listNotifications(
    @Headers("authorization") authorization: string,
    @Query("page") page: number = 1,
    @Query("limit") limit: number = 10,
  ) {
    const token = authorization?.replace("Bearer ", "")
    return this.notificationService.listNotifications(
      token,
      Number(page),
      Number(limit),
    )
  }
}
