import {
  Body,
  Controller,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
  UploadedFiles,
  UseInterceptors,
} from '@nestjs/common';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger';

import { CreateInspectionReportDto } from './dto/create-inspection-report.dto';
import { UpdateInspectionReportDto } from './dto/update-inspection-report.dto';
import { WarehouseInspectionService } from './warehouse-inspection.service';

@ApiTags('warehouse-inspection')
@ApiBearerAuth()
@Controller()
export class WarehouseInspectionController {
  constructor(private readonly inspectionService: WarehouseInspectionService) {}

  @Post('warehouse-inventory/:inventoryId/inspection-reports')
  @ApiOperation({ summary: 'Create an inspection / damage report for an inventory item' })
  create(
    @Param('inventoryId', ParseUUIDPipe) inventoryId: string,
    @Body() dto: CreateInspectionReportDto,
  ) {
    return this.inspectionService.create(inventoryId, dto);
  }

  @Get('warehouse-inventory/:inventoryId/inspection-reports')
  @ApiOperation({ summary: 'List inspection reports for an inventory item' })
  listByInventory(@Param('inventoryId', ParseUUIDPipe) inventoryId: string) {
    return this.inspectionService.findByInventory(inventoryId);
  }

  @Get('warehouse-inspection-reports/:id')
  @ApiOperation({ summary: 'Get an inspection report (with attachments)' })
  async findOne(@Param('id', ParseUUIDPipe) id: string) {
    const report = await this.inspectionService.findById(id);
    const attachments = await this.inspectionService.listAttachments(id);
    return { ...report, attachments };
  }

  @Patch('warehouse-inspection-reports/:id')
  @ApiOperation({ summary: 'Update an inspection report' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateInspectionReportDto) {
    return this.inspectionService.update(id, dto);
  }

  @Post('warehouse-inspection-reports/:id/attachments')
  @UseInterceptors(AnyFilesInterceptor())
  @ApiConsumes('multipart/form-data')
  @ApiOperation({ summary: 'Upload inspection images / documents' })
  addAttachments(
    @Param('id', ParseUUIDPipe) id: string,
    @UploadedFiles() files: Express.Multer.File[],
  ) {
    return this.inspectionService.addAttachments(id, files);
  }
}
