import { Body, Controller, Get, Post, Patch, Delete, Param, Query, UseGuards } from '@nestjs/common';
import { ChannelsService } from './channels.service';
import { JwtAuthGuard } from '../../common/guards/jwt.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Workspace } from '../../common/decorators/workspace.decorator';
import { CreateChannelDto, UpdateChannelDto, InviteToChannelDto, CreateBookmarkDto, PinMessageDto } from './dto';

@Controller('channels')
@UseGuards(JwtAuthGuard)
export class ChannelsController {
  constructor(private readonly channelsService: ChannelsService) {}

  @Get()
  async list(@Workspace('id') wsId: string, @CurrentUser('userId') userId: string, @Query() query: any) {
    return this.channelsService.listChannels(wsId, userId, query);
  }

  @Post()
  async create(@Workspace('id') wsId: string, @CurrentUser('userId') userId: string, @Body() dto: CreateChannelDto) {
    return this.channelsService.create(wsId, userId, dto);
  }

  @Get(':id')
  async get(@Param('id') id: string, @Workspace('id') wsId: string) {
    return this.channelsService.findById(id, wsId);
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
    @Body() dto: UpdateChannelDto,
  ) {
    return this.channelsService.update(id, wsId, userId, dto);
  }

  @Post(':id/join')
  async join(@Param('id') id: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string) {
    return this.channelsService.join(id, userId, wsId);
  }

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

  @Post(':id/invite')
  async invite(
    @Param('id') id: string,
    @Body() dto: InviteToChannelDto,
    @Workspace('id') wsId: string,
    @CurrentUser('userId') userId: string,
  ) {
    return this.channelsService.invite(id, dto.user_ids, wsId, userId);
  }

  @Get(':id/members')
  async members(@Param('id') id: string, @Workspace('id') wsId: string) {
    return this.channelsService.listMembers(id, wsId);
  }

  @Post(':id/archive')
  async archive(@Param('id') id: string, @CurrentUser('userId') userId: string, @Workspace('id') wsId: string) {
    return this.channelsService.archive(id, userId, wsId);
  }

  @Get(':id/messages')
  async messages(@Param('id') id: string, @Query() query: any, @Workspace('id') wsId: string) {
    return this.channelsService.getMessages(id, query, wsId);
  }

  // --- Bookmarks ---

  @Get(':id/bookmarks')
  async listBookmarks(@Param('id') channelId: string, @Workspace('id') wsId: string) {
    return this.channelsService.listBookmarks(channelId, wsId);
  }

  @Post(':id/bookmarks')
  async createBookmark(
    @Param('id') channelId: string,
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
    @Body() dto: CreateBookmarkDto,
  ) {
    return this.channelsService.createBookmark(channelId, userId, wsId, dto);
  }

  @Delete(':id/bookmarks/:bookmarkId')
  async deleteBookmark(
    @Param('id') channelId: string,
    @Param('bookmarkId') bookmarkId: string,
    @Workspace('id') wsId: string,
  ) {
    return this.channelsService.deleteBookmark(channelId, bookmarkId, wsId);
  }

  // --- Pins ---

  @Get(':id/pins')
  async listPins(@Param('id') channelId: string, @Workspace('id') wsId: string) {
    return this.channelsService.listPins(channelId, wsId);
  }

  @Post(':id/pins')
  async createPin(
    @Param('id') channelId: string,
    @CurrentUser('userId') userId: string,
    @Workspace('id') wsId: string,
    @Body() dto: PinMessageDto,
  ) {
    return this.channelsService.createPin(channelId, userId, dto.message_id, wsId);
  }

  @Delete(':id/pins/:messageId')
  async deletePin(
    @Param('id') channelId: string,
    @Param('messageId') messageId: string,
    @Workspace('id') wsId: string,
  ) {
    return this.channelsService.deletePin(channelId, messageId, wsId);
  }
}