import { Controller, Get, Param, ParseUUIDPipe, Res } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { Response } from "express";

import { FilesService } from "./files.service";

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

  @Get(":fileId")
  @ApiOperation({
    summary: "Download a file by ID",
    description:
      "Global endpoint — streams any uploaded file directly from MinIO by its UUID. " +
      "No resource context (e.g. booking ID) required.",
  })
  async download(
    @Param("fileId", ParseUUIDPipe) fileId: string,
    @Res() res: Response,
  ) {
    const { stream, record } = await this.filesService.streamById(fileId);
    res.setHeader("Content-Type", record.mimeType);
    res.setHeader("Content-Disposition", `attachment; filename="${record.name}"`);
    stream.pipe(res);
  }
}
