import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Put,
  UploadedFiles,
  UseInterceptors,
  ValidationPipe,
} from '@nestjs/common';
import { DevicesService } from './devices.service';
import { CreateDeviceDto } from './dtos/CreateDeviceDto.dto';
import { AnyFileUpload } from 'src/interceptors/file-upload.interceptor';
import { AddAttachmentDto } from '../visa-commission/dto/add-attachment.dto';
import { MulterFile } from 'src/constants/types/multer-file.type';
import { handleServiceRequest } from 'src/utils/response/response-handler';
import { RemoveAttachmentDto } from '../visa-commission/dto/remove-attachment.dto';

@Controller('')
export class DevicesController {
  constructor(private service: DevicesService) {}

  @Get('count')
  getTotalDevices() {
    return this.service.getTotalDevices();
  }

  @Get('vendor-devices')
  getVendorDeviceChart() {
    return this.service.getVendorDeviceChart();
  }

  @Get('device-status')
  getDeviceStatusChart() {
    return this.service.getDeviceStatusChart();
  }

  @Post()
  create(@Body() dto: any) {
    return this.service.create(dto);
  }

  @Get('')
  findAlld() {
    // return this.service.findAlls();
  }

  // @Post('all')
  // deviceAssignment(@Body() dto: any) {
  //   return this.service.deviceAssignment(dto);
  // }

  @Post('all')
  findAll(@Body() dto: any) {
    return this.service.findAll(dto);
  }

  @Put()
  edit(@Body() editVendorDto: any) {
    return this.service.edit(editVendorDto.id, editVendorDto);
  }

  @Get(':id')
  findOne(@Param('id') id: number) {
    return this.service.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: number, @Body() dto: any) {
    return this.service.update(+id, dto);
  }

  @Delete(':id')
  remove(@Param('id') id: number) {
    return this.service.remove(+id);
  }

  // ==================== ATTACHMENT MANAGEMENT ENDPOINTS ====================

  /**
   * GET /view-attachments/:id - Retrieves attachments for a specific bonus receipt
   *
   * @param id - Bonus receipt ID
   * @returns List of file attachments with metadata
   */
  @Get('view-attachments/:id')
  async getAttachments(@Param('id') id: string) {
    return await handleServiceRequest(this.service.getAttachments(id));
  }

  /**
   * POST /remove-attachment - Removes an attachment from a bonus receipt
   *
   * @param attachment - Contains bonus receipt ID and file name to remove
   * @param userData - Current user's data for audit trail
   * @returns Removal result
   */
  @Post('remove-attachment')
  async removeAttachments(
    @Body(new ValidationPipe()) attachment: RemoveAttachmentDto,
    // @UserData() userData: UserDataProps,
  ) {
    return await handleServiceRequest(
      this.service.removeAttachments(attachment, {}),
    );
  }

  /**
   * POST /add-attachment - Adds new attachments to an existing bonus receipt
   *
   * @param body - Contains bonus receipt ID and file metadata
   * @param files - Uploaded files to attach
   * @param userData - Current user's data for audit trail
   * @returns Added attachments result
   */
  @Post('add-attachment')
  @UseInterceptors(AnyFileUpload())
  async addAttachments(
    @Body(new ValidationPipe()) body: AddAttachmentDto,
    @UploadedFiles() files: MulterFile[],
    // @UserData() userData: UserDataProps,
  ) {
    // Map uploaded files to filenames
    body.attachment_file = files?.map((file) => file.filename) || null;
    return await handleServiceRequest(this.service.addAttachments(body, {}));
  }
}
