import { Body, Controller, HttpCode, HttpStatus, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Throttle } from '../../common/decorators/throttle.decorator';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { MediaStorageService } from '../../common/media/media-storage.service';
import { CreateDirectUploadDto } from './dto/create-direct-upload.dto';

@ApiTags('Media')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('media/uploads')
export class DirectMediaUploadController {
  constructor(private readonly mediaStorageService: MediaStorageService) {}

  @Post('presign')
  @HttpCode(HttpStatus.OK)
  @Throttle(30, 60_000)
  @ApiOperation({ summary: 'Create a short-lived S3 PUT URL for a direct media upload' })
  createPresignedUpload(@CurrentUser() user: JwtPayload, @Body() dto: CreateDirectUploadDto) {
    return this.mediaStorageService.createPresignedUpload({
      userId: user.sub,
      folder: dto.folder,
      mimeType: dto.mimeType,
      size: dto.size,
    });
  }
}
