import { Controller, Get, Post, Delete, Body, Param } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { FilesService } from './files.service';
import { CreateUploadUrlDto, CompleteUploadDto } from './dto';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { FileUploadRateLimit } from '../../common/decorators/rate-limit.decorator';
import type { UserPayload } from '../../common/types';

@ApiTags('files')
@Controller('files')
@ApiBearerAuth()
export class FilesController {
  constructor(private readonly filesService: FilesService) {}

  @Post('upload-url')
  @FileUploadRateLimit()
  @ApiOperation({ summary: '获取预签名上传 URL' })
  async createUploadUrl(@CurrentUser() user: UserPayload, @Body() dto: CreateUploadUrlDto) {
    return this.filesService.requestUploadUrl(user.id, dto);
  }

  @Post('complete')
  @FileUploadRateLimit()
  @ApiOperation({ summary: '确认上传完成' })
  async completeUpload(@CurrentUser() user: UserPayload, @Body() dto: CompleteUploadDto) {
    const result = await this.filesService.confirmUpload(user.id, dto);
    return result;
  }

  @Get(':id')
  @ApiOperation({ summary: '获取文件信息及下载 URL' })
  async getFile(@CurrentUser() user: UserPayload, @Param('id') id: string) {
    return this.filesService.getFile(user.id, id);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除文件（COS + 数据库）' })
  async deleteFile(@CurrentUser() user: UserPayload, @Param('id') id: string) {
    return this.filesService.deleteFile(user.id, id);
  }
}
