import { Controller, Get, Put, Param, Query, UseGuards, Req, HttpCode } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { MessageService } from '../../../application/services/message.service';

@ApiTags('Messages')
@Controller('messages')
export class MessageController {
  constructor(private readonly messageService: MessageService) {}

  @Get()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get messages list' })
  async getMessages(
    @Req() req: any,
    @Query('page') page: string = '1',
    @Query('limit') limit: string = '20',
  ) {
    const result = await this.messageService.getMessages(req.user.id, parseInt(page), parseInt(limit));
    return { code: 0, data: result };
  }

  @Get('unread-count')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get unread message count' })
  async getUnreadCount(@Req() req: any) {
    const count = await this.messageService.getUnreadCount(req.user.id);
    return { code: 0, data: { count } };
  }

  @Put(':id/read')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @HttpCode(200)
  @ApiOperation({ summary: 'Mark message as read' })
  async markAsRead(@Param('id') id: string, @Req() req: any) {
    await this.messageService.markAsRead(id, req.user.id);
    return { code: 0, data: null };
  }

  @Put('read-all')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @HttpCode(200)
  @ApiOperation({ summary: 'Mark all messages as read' })
  async markAllAsRead(@Req() req: any) {
    await this.messageService.markAllAsRead(req.user.id);
    return { code: 0, data: null };
  }
}
