import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Put,
  UploadedFile,
  UploadedFiles,
  UseInterceptors,
  ValidationPipe,
} from '@nestjs/common';
import { VendorsService } from './vendors.service';
import { CreateVendorDto } from './dtos/CreateVendorDto.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';
import { DEFAULT_USER_ID } from 'src/constants/auth';
import { FileInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { extname } from 'path';
import { ImportService } from 'src/modules/import/import.service';

@Controller('')
export class VendorsController {
  constructor(
    private service: VendorsService,
    private readonly importService: ImportService,
  ) {}

  @Get('run')
  runImport() {
    return this.importService.importFromLocal();
  }

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

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

  // @Get()
  // findAll() {
  //   return this.service.findAll();
  // }

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

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

  @Post('update-status')
  async updateStatus(
    @Body() body: any,
    // @SessionData() sessionData: SessionDataProps,
  ) {
    return await this.service.updateStatus(String(DEFAULT_USER_ID), body);
  }

  @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, {}));
  }
}
