import { type File, FileInterceptor, FilesInterceptor } from '@nest-lab/fastify-multer';
import { Controller, Post, UploadedFile, UploadedFiles, UseGuards, UseInterceptors } from '@nestjs/common';
import { ApiBody, ApiConsumes, ApiOperation, ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { UploadMultipleFileDto, UploadSingleFileDto } from '../DTO/upload-file.dto';
import { UploaderService } from '../providers/uploader.service';
import { AdminAuthGuard } from '../../auth/guards/adminAuth.guard';
import { AuthGuard } from '../../auth/guards/auth.guard';

@ApiTags('uploader')
@Controller()
export class UploaderController {
  constructor(private readonly uploaderService: UploaderService) {}

  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Upload a file (admin)' })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FileInterceptor('file'))
  @ApiBody({ type: UploadSingleFileDto })
  @Post('public/single-file')
  uploadFile(@UploadedFile() file: File) {
    return this.uploaderService.uploadFile(file);
  }

  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Uploads multiple files' })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FilesInterceptor('files'))
  @ApiBody({ type: UploadMultipleFileDto })
  @Post('public/multi-file')
  uploadFiles(@UploadedFiles() files: File[]) {
    return this.uploaderService.uploadMultiple(files);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Upload a file (admin)' })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FileInterceptor('file'))
  @ApiBody({ type: UploadSingleFileDto })
  @Post('admin/single-file')
  uploadFileAdmin(@UploadedFile() file: File) {
    return this.uploaderService.uploadFile(file);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Uploads multiple files' })
  @ApiConsumes('multipart/form-data')
  @UseInterceptors(FilesInterceptor('files'))
  @ApiBody({ type: UploadMultipleFileDto })
  @Post('admin/multi-file')
  uploadFilesAdmin(@UploadedFiles() files: File[]) {
    return this.uploaderService.uploadMultiple(files);
  }
}
