import { Body, Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';
import { FileInterceptor } from '@nestjs/platform-express';
import { memoryStorage } from 'multer';
import { QuickImportDto } from './dto/quick-import.dto';
import { QuickImportService, QuickImportResult } from './quick-import.service';

@Controller('quick-import')
export class QuickImportController {
  constructor(private readonly quickImportService: QuickImportService) {}

  @Post()
  @Throttle({ default: { ttl: 60_000, limit: 20 } })
  @UseInterceptors(
    FileInterceptor('file', {
      storage: memoryStorage(),
      limits: { fileSize: 10 * 1024 * 1024 },
    }),
  )
  async importFromInput(
    @Body() body: QuickImportDto,
    @UploadedFile() file?: Express.Multer.File,
  ): Promise<QuickImportResult> {
    if (file) {
      return this.quickImportService.importFromUpload(file);
    }

    return this.quickImportService.importFromInput(body?.input ?? '');
  }
}
