import { Body, Controller, Get, Param, Post, Query, Req, UseGuards } from '@nestjs/common';
import type { Request } from 'express';
import { AuthenticatedUser } from '../auth/auth.types';
import { AuthGuard } from '../common/auth.guard';
import { SendConversationMessageDto } from './dto/send-message.dto';
import { ConversationsService } from './conversations.service';

@UseGuards(AuthGuard)
@Controller('conversations')
export class ConversationsController {
  constructor(private readonly conversationsService: ConversationsService) {}

  @Get()
  findAll(@Query('filter') filter?: string, @Query('search') search?: string) {
    return this.conversationsService.findAll({ filter, search });
  }

  @Get(':contactId')
  findOne(@Param('contactId') contactId: string) {
    return this.conversationsService.findOne(contactId);
  }

  @Post(':contactId/messages')
  sendMessage(
    @Req() request: Request & { user: AuthenticatedUser },
    @Param('contactId') contactId: string,
    @Body() dto: SendConversationMessageDto,
  ) {
    return this.conversationsService.sendMessage(contactId, dto, request.user, request.ip);
  }

  @Post(':contactId/assign')
  assignToCurrentUser(
    @Req() request: Request & { user: AuthenticatedUser },
    @Param('contactId') contactId: string,
  ) {
    return this.conversationsService.assignToCurrentUser(contactId, request.user, request.ip);
  }
}
