import { Body, Controller, Get, Post, Param, UseGuards } from '@nestjs/common';
import { ConnectService } from './connect.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Workspace } from '../../common/decorators/workspace.decorator';

@Controller('connect')
@UseGuards(JwtAuthGuard)
export class ConnectController {
  constructor(private readonly connectService: ConnectService) {}

  @Post('invite')
  async createInvite(
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
    @Body() dto: any,
  ) {
    return this.connectService.createSharedChannelInvite({
      channelId: dto.channel_id,
      sourceWorkspaceId: wsId,
      targetWorkspaceId: dto.target_workspace_id,
      targetChannelName: dto.target_channel_name,
      targetChannelTopic: dto.target_channel_topic,
      targetIsPrivate: dto.target_is_private,
      createdBy: userId,
    });
  }

  @Get('incoming')
  async listIncoming(@Workspace('id') wsId: string) {
    return this.connectService.listIncomingInvites(wsId);
  }

  @Get('outgoing')
  async listOutgoing(@Workspace('id') wsId: string) {
    return this.connectService.listOutgoingInvites(wsId);
  }

  @Get('active')
  async listActive(@Workspace('id') wsId: string) {
    return this.connectService.listActiveShared(wsId);
  }

  @Post(':id/accept')
  async acceptInvite(
    @Param('id') id: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.connectService.acceptInvite(id, userId);
  }

  @Post(':id/reject')
  async rejectInvite(
    @Param('id') id: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.connectService.rejectInvite(id, userId);
  }

  @Post(':id/disconnect')
  async disconnect(
    @Param('id') id: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.connectService.disconnect(id, userId);
  }
}