import {
  Body,
  Controller,
  Get,
  HttpStatus,
  Param,
  Post,
  Query,
  Res,
  UseGuards,
  UsePipes,
} from '@nestjs/common';
import { Response } from 'express';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import { JwtAuthGuard } from '../owner/guards/jwt.guard';
import {
  NotificationFilterDto,
  notificationFilterSchema,
  NotificationMarkAsReadDto,
  notificationMarkAsReadSchema,
} from './notifications.dto';
import { NotificationsService } from './notifications.service';

@Controller('notifications')
export class NotificationsController {
  constructor(private readonly service: NotificationsService) {}

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(notificationFilterSchema))
  @Get('all/:memberId')
  async findByMember(
    @Param('memberId') memberId: string,
    @Query() query: NotificationFilterDto,
    @Res() res: Response,
  ) {
    const result = await this.service.getNotifications(query, memberId);
    return res.status(HttpStatus.OK).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @UsePipes(new ZodValidationPipe(notificationMarkAsReadSchema))
  @Post('mark-as-read')
  async markAsRead(
    @Body() data: NotificationMarkAsReadDto,
    @Res() res: Response,
  ) {
    const result = await this.service.markAsRead(data);
    return res.status(HttpStatus.CREATED).json(result);
  }

  @UseGuards(JwtAuthGuard)
  @Post('mark-all-as-read/:member_id')
  async markAllAsRead(
    @Param('member_id') member_id: string,
    @Res() res: Response,
  ) {
    const result = await this.service.markAllAsRead(member_id);
    return res.status(HttpStatus.CREATED).json(result);
  }
}
