import { Body, Controller, Get, Param, Post } from "@nestjs/common";
import { CurrentUser } from "../../common/decorators/current-user.decorator";
import { ConversationsService } from "./conversations.service";

@Controller("conversations")
export class ConversationsController {
  constructor(private readonly conversations: ConversationsService) {}

  @Get()
  list(@CurrentUser() user: { sub: string }) {
    return this.conversations.list(user.sub);
  }

  @Post("open")
  open(@CurrentUser() user: { sub: string }, @Body() body: { peerId: string; requestId?: string; adId?: string }) {
    return this.conversations.open(user.sub, body.peerId, body.requestId, body.adId);
  }

  @Get(":id")
  detail(@CurrentUser() user: { sub: string }, @Param("id") id: string) { return this.conversations.detail(id, user.sub); }

  @Post(":id/messages")
  sendMessage(@CurrentUser() user: { sub: string }, @Param("id") id: string, @Body() body: { content?: string; type?: string; metadata?: Record<string, unknown> }) {
    return this.conversations.sendMessage(user.sub, id, body);
  }

  @Get(":id/messages")
  messages(@CurrentUser() user: { sub: string }, @Param("id") id: string) {
    return this.conversations.messages(id, user.sub);
  }

  @Post(":id/appointments/:messageId/respond")
  respondAppointment(@CurrentUser() user: { sub: string }, @Param("id") id: string, @Param("messageId") messageId: string, @Body("accepted") accepted: boolean) {
    return this.conversations.respondAppointment(id, messageId, user.sub, accepted);
  }
}
