import { Controller, Get, Param, Patch, Req, UseGuards } from "@nestjs/common";
import { Request } from "express";
import { JwtAuthGuard } from "./jwt-auth.guard";
import { JwtPayload } from "./auth.service";
import { NotificationsService } from "./notifications.service";

@Controller("notifications")
@UseGuards(JwtAuthGuard)
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}

  @Get()
  getAll(@Req() req: Request & { user: JwtPayload }) {
    return this.notificationsService.getForUser(req.user.sub);
  }

  @Get("unread-count")
  getUnreadCount(@Req() req: Request & { user: JwtPayload }) {
    return this.notificationsService.getUnreadCount(req.user.sub).then((count) => ({ count }));
  }

  @Patch(":id/read")
  markRead(
    @Param("id") id: string,
    @Req() req: Request & { user: JwtPayload },
  ) {
    return this.notificationsService.markRead(id, req.user.sub).then(() => ({ ok: true }));
  }

  @Patch("read-all")
  markAllRead(@Req() req: Request & { user: JwtPayload }) {
    return this.notificationsService.markAllRead(req.user.sub).then(() => ({ ok: true }));
  }
}
