import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  UseGuards,
} from '@nestjs/common';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Roles } from '../../common/decorators/roles.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { RolesGuard } from '../../common/guards/roles.guard';
import type { AuthUser } from '../../common/types/auth-user.type';
import { CreateNotificationDto } from './dto/create-notification.dto';
import { UpdateNotificationDto } from './dto/update-notification.dto';
import { NotificationsService } from './notifications.service';

@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('super_admin', 'agency_manager', 'agent', 'driver')
@Controller('notifications')
export class NotificationsController {
  constructor(private readonly notificationsService: NotificationsService) {}

  @Post()
  @Roles('super_admin', 'agency_manager')
  create(
    @Body() data: CreateNotificationDto,
    @CurrentUser() user: AuthUser,
  ) {
    return this.notificationsService.createForUser(data, user);
  }

  @Get()
  findAll(@CurrentUser() user: AuthUser) {
    return this.notificationsService.findAllForUser(user);
  }

  @Get('unread')
  unread(@CurrentUser() user: AuthUser) {
    return this.notificationsService.unreadForUser(user);
  }

  @Get(':id')
  findOne(
    @Param('id') id: string,
    @CurrentUser() user: AuthUser,
  ) {
    return this.notificationsService.findOneForUser(Number(id), user);
  }

  @Patch(':id')
  @Roles('super_admin', 'agency_manager')
  update(
    @Param('id') id: string,
    @Body() data: UpdateNotificationDto,
    @CurrentUser() user: AuthUser,
  ) {
    return this.notificationsService.updateForUser(Number(id), data, user);
  }

  @Patch(':id/read')
  markRead(
    @Param('id') id: string,
    @CurrentUser() user: AuthUser,
  ) {
    return this.notificationsService.markReadForUser(Number(id), user);
  }

  @Patch(':id/unread')
  markUnread(
    @Param('id') id: string,
    @CurrentUser() user: AuthUser,
  ) {
    return this.notificationsService.markUnreadForUser(Number(id), user);
  }

  @Delete(':id')
  @Roles('super_admin', 'agency_manager')
  remove(
    @Param('id') id: string,
    @CurrentUser() user: AuthUser,
  ) {
    return this.notificationsService.removeForUser(Number(id), user);
  }
}