import { Controller, UseInterceptors, Post, Get, Param, Res, UploadedFile, Body, Delete, Query } from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";

import { memoryStorage } from 'multer';
import { Response } from 'express';
import { AdminSearchDto } from "src/time-and-attendance/attachments/dtos/search-filters.dto";
import { UploadMetaAttachmentsDto } from "src/time-and-attendance/attachments/dtos/upload-meta-attachments.dto";
import { maxUploadBytes } from "src/time-and-attendance/attachments/upload.config";
import { AttachmentDeleteService } from "src/time-and-attendance/attachments/services/attachment-delete.service";
import { AttachmentGetService } from "src/time-and-attendance/attachments/services/attachment-get.service";
import { AttachmentUploadService } from "src/time-and-attendance/attachments/services/attachment-upload.service";

@Controller('attachments')
export class AttachmentsController {
    constructor(
        private readonly uploadService: AttachmentUploadService,
        private readonly deleteService: AttachmentDeleteService,
        private readonly getService: AttachmentGetService,
    ) { }

    @Get(':id')
    async getById(@Param('id') id: string, @Query('variant') variant: string | undefined, @Res() res: Response) {
        return await this.getService.findAttachmentById(id, variant, res)
    }

    @Get('variants/:id')
    async getlistVariantsById(@Param('id') id: string) {
        return await this.getService.getListVariants(id);
    }

    @Delete(':id')
    async remove(@Param('id') id: string) {
        return await this.deleteService.deleteAttachment(id);
    }

    @Post()
    @UseInterceptors(FileInterceptor('file', { storage: memoryStorage(), limits: { fileSize: maxUploadBytes() } }))
    async upload(@UploadedFile() file?: Express.Multer.File, @Body() meta?: UploadMetaAttachmentsDto) {
        return await this.uploadService.uploadAttachment(file, meta);
    }

    @Get('search/filters')
    async searchWithFilters(@Query() dto: AdminSearchDto) {
        return await this.getService.searchAttachmentWithFilters(dto);
    }
}
