import { Controller, Get, Post, Delete, Param, Res, UploadedFile, UseInterceptors, Body, Query } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import type { Response } from 'express';
import { DocumentosService } from './documentos.service';

@Controller()
export class DocumentosController {
  constructor(private svc: DocumentosService) {}

  @Get('demandas/:id/documentos')
  findByDemanda(@Param('id') demandaId: string) {
    return this.svc.findByDemanda(demandaId);
  }

  @Post('demandas/:id/documentos')
  @UseInterceptors(FileInterceptor('file', { limits: { fileSize: 50 * 1024 * 1024 } }))
  upload(
    @Param('id') demandaId: string,
    @UploadedFile() file: Express.Multer.File,
    @Body('tipo') tipo?: string,
  ) {
    return this.svc.upload(demandaId, file, tipo);
  }

  @Get('documentos/:id/download')
  async download(@Param('id') id: string, @Res() res: Response) {
    const doc = await this.svc.findOne(id);
    const filePath = this.svc.getFilePath(doc);
    res.download(filePath, doc.nome_arquivo);
  }

  @Delete('documentos/:id')
  remove(@Param('id') id: string) {
    return this.svc.remove(id);
  }
}
