import { Controller, Get, HttpException, InternalServerErrorException, Param, StreamableFile, UseGuards, } from "@nestjs/common"; import { ApiBearerAuth, ApiOperation, ApiParam, ApiProduces, ApiResponse, ApiTags, } from "@nestjs/swagger"; import { LocalActorAuthGuard } from "src/auth/guards/actor-local.guard"; import { RolesGuard } from "src/auth/guards/role.guard"; import { Roles } from "src/decorators/roles.decorator"; import { CurrentUser } from "src/decorators/user.decorator"; import { RoleEnum } from "src/Types&Enums/role.enum"; import { CaseExpertReportService } from "./case-expert-report.service"; @ApiTags("expert-insurer-panel") @Controller("expert-insurer") @ApiBearerAuth() @UseGuards(LocalActorAuthGuard, RolesGuard) @Roles(RoleEnum.COMPANY) export class CaseExpertReportInsurerController { constructor( private readonly caseExpertReportService: CaseExpertReportService, ) {} @Get("files/:publicId/report.pdf") @ApiOperation({ summary: "Download insurer file report PDF", description: "Generates a PDF for the shared publicId (blame + claim combined): damaged owner, driver when different, insurance, vehicle, and accident report sections.", }) @ApiParam({ name: "publicId" }) @ApiProduces("application/pdf") @ApiResponse({ status: 200, description: "PDF file" }) @ApiResponse({ status: 404, description: "File not found for this publicId" }) async downloadInsurerReport( @CurrentUser() insurer: { clientKey?: string }, @Param("publicId") publicId: string, ): Promise { try { const { buffer, filename } = await this.caseExpertReportService.generateForInsurer( publicId, insurer, ); return new StreamableFile(buffer, { type: "application/pdf", disposition: `attachment; filename="${filename}"`, }); } catch (error) { if (error instanceof HttpException) throw error; throw new InternalServerErrorException( error instanceof Error ? error.message : "Failed to generate insurer file report PDF", ); } } }