import {
  Body,
  Controller,
  Param,
  Post,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MEDIA_MAX_SIZE_BYTES } from '../../common/media/allowed-media';
import { ApiBearerAuth, ApiConsumes, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { CollaborationRequestsService } from './collaboration-requests.service';
import { CreateGeneralCollaborationRequestDto } from './dto/create-general-collaboration-request.dto';

type UploadedAudioFile = {
  buffer: Buffer;
  originalname: string;
  mimetype: string;
  size: number;
};

@ApiTags('User Collaboration Requests')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('users')
export class UsersCollaborationRequestsController {
  constructor(private readonly collaborationRequestsService: CollaborationRequestsService) {}

  @Post(':targetUserId/collaboration-requests')
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(
    FileInterceptor('attachmentUrl', {
      limits: { fileSize: MEDIA_MAX_SIZE_BYTES.collaborationAudio, files: 1 },
    }),
  )
  async createGeneral(
    @CurrentUser() user: JwtPayload,
    @Param('targetUserId') targetUserId: string,
    @Body() dto: CreateGeneralCollaborationRequestDto,
    @UploadedFile() attachmentUrl?: UploadedAudioFile,
  ) {
    return this.collaborationRequestsService.createGeneral(
      user.sub,
      targetUserId,
      dto,
      attachmentUrl,
    );
  }
}
