import { Controller, Get, Param, Patch, Post, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
import { NotificationsService } from './notifications.service';
import { JwtGuard } from '../../common/jwt.guard';
import { IamGuard, IamRoles } from '../../common/iam-adapter';
import { TestNotificationDto } from './notifications.dto';
import { EmailClientService } from './email-client.service';
import { SmsClientService } from './sms-client.service';
import { SendEmail } from './dtos/email.dto';
import { BulkMessagesDto, SingleMessageDto } from './dtos/sms.dto';

@ApiTags('Notifications')
@Controller('notifications')
@UseGuards(JwtGuard)
@ApiBearerAuth('JWT-auth')
export class NotificationsController {
  constructor(
    private service: NotificationsService,
    private emailClient: EmailClientService,
    private smsClient: SmsClientService,
  ) {}

  @Get(':passengerId')
  @ApiOperation({ summary: 'Get notifications for passenger' })
  getForPassenger(@Param('passengerId') id: string) {
    return this.service.getForPassenger(id);
  }

  @Patch(':id/read')
  @ApiOperation({ summary: 'Mark notification as read' })
  markRead(@Param('id') id: string) {
    return this.service.markRead(id);
  }

  @Patch(':passengerId/read-all')
  @ApiOperation({ summary: 'Mark all notifications as read' })
  markAllRead(@Param('passengerId') id: string) {
    return this.service.markAllRead(id);
  }

  @Post('send/email')
  @UseGuards(IamGuard)
  @IamRoles('ADMIN', 'STAFF')
  @ApiOperation({ summary: 'Send a direct email via the email microservice' })
  @ApiBody({ type: SendEmail })
  sendEmail(@Body() dto: SendEmail) {
    return this.emailClient.sendEmail(dto);
  }

  @Post('send/sms')
  @UseGuards(IamGuard)
  @IamRoles('ADMIN', 'STAFF')
  @ApiOperation({ summary: 'Send a direct SMS via the SMS microservice' })
  @ApiBody({ type: SingleMessageDto })
  sendSms(@Body() dto: SingleMessageDto) {
    return this.smsClient.sendSms(dto);
  }

  @Post('send/sms/bulk')
  @UseGuards(IamGuard)
  @IamRoles('ADMIN', 'STAFF')
  @ApiOperation({ summary: 'Send bulk SMS messages via the SMS microservice' })
  @ApiBody({ type: BulkMessagesDto })
  sendBulkSms(@Body() dto: BulkMessagesDto) {
    return this.smsClient.sendBulkMessages(dto);
  }

  @Post('test')
  @UseGuards(IamGuard)
  @IamRoles('ADMIN', 'STAFF')
  @ApiOperation({ summary: 'Test notification delivery (Admin only)' })
  async testNotification(@Body() dto: TestNotificationDto) {
    return this.service.send(
      dto.templateKey,
      dto.recipient,
      dto.context,
      dto.channels as any,
    );
  }
}
