import { Controller, Get, Param, Post, Res, UseGuards } from "@nestjs/common";
import type { Response } from "express";
import { SkipCsrf } from "../common/decorators/skip-csrf.decorator";
import { ApiKeyGuard } from "../common/guards/api-key.guard";
import {
  QueueNotificationsService,
  type QueueNotification,
  type QueueTask,
} from "./queue-notifications.service";

@Controller("queue")
@UseGuards(ApiKeyGuard)
export class QueueNotificationsController {
  constructor(private readonly queueNotificationsService: QueueNotificationsService) {}

  @Get("notifications")
  async getNotifications(): Promise<QueueNotification[]> {
    return this.queueNotificationsService.listNotifications();
  }

  @Get("notifications/stream")
  async streamNotifications(@Res() res: Response): Promise<void> {
    await this.queueNotificationsService.streamNotifications(res);
  }

  @SkipCsrf()
  @Post("notifications/:id/ack")
  async ackNotification(@Param("id") id: string): Promise<{ success: true; id: string }> {
    return this.queueNotificationsService.ackNotification(id);
  }

  @Get("tasks")
  async getTasks(): Promise<QueueTask[]> {
    return this.queueNotificationsService.listTasks();
  }
}
