import {
  Controller,
  Post,
  Body,
  UseInterceptors,
  UploadedFile,
  BadRequestException,
  UseGuards,
} from "@nestjs/common";
import { FileUploadService } from "./file-upload.service";
import { FileInterceptor } from "@nestjs/platform-express";
import {
  S3,
  PutObjectCommand,
  GetObjectCommand,
  DeleteObjectCommand,
} from "@aws-sdk/client-s3";
import { awsConfig } from "src/aws.config";
import { fileUpload } from "./entities/file-upload.entity";
import { ThrottlerGuard, SkipThrottle } from "@nestjs/throttler";

@Controller("file_upload")
export class FileUploadController {
  constructor(private readonly fileUploadService: FileUploadService) { }

  @Post()
  @UseInterceptors(FileInterceptor("file"))
  async create(@UploadedFile() file: Express.Multer.File) {
    try {
      if (file) {
        const s3 = new S3({
          credentials: {
            accessKeyId: awsConfig.accessKeyId,
            secretAccessKey: awsConfig.secretAccessKey,
          },
          region: awsConfig.region,
        });
        let dongri = await `${file.originalname}_${Date.now()}`;
        const command = new PutObjectCommand({
          Bucket: awsConfig.bucketName,
          Key: dongri,
          Body: file.buffer,
        });

        const data = await s3.send(command);
        let a = data.$metadata.requestId;

        let response = {
          status: process.env.SUCCESS_STATUS_ABHA_ADDRESS_MAPPING,
          message: process.env.FILE_UPLOAD_SUCCESS_RESP,
          data: dongri,
        };
        return response;
      }
    } catch (error) {
      console.error(error);
      let response = {
        status: process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        message: process.env.FILE_UPLOAD_FAILED_RESP,
      };
      return response;
    }
  }
  @UseGuards(ThrottlerGuard)
  @SkipThrottle()
  @Post("/getDocs")
  async getDocs(@Body() PatientEntity: fileUpload) {
    try {

      const s3 = new S3({
        credentials: {
          accessKeyId: awsConfig.accessKeyId,
          secretAccessKey: awsConfig.secretAccessKey,
        },
        region: awsConfig.region,
      });

      const command = new GetObjectCommand({
        Bucket: awsConfig.bucketName,
        Key: PatientEntity.value,
      });

      const s3Data = await s3.send(command);
      const buffer = Buffer.from(await s3Data.Body.transformToByteArray());
      return buffer.toString("base64");
    } catch (error) {
      console.error(error);
      return error;
    }
  }
  @UseGuards(ThrottlerGuard)
  @SkipThrottle()
  @Post("/delDocs")
  async delDocs(@Body() PatientEntity: fileUpload) {
    try {
      const s3 = new S3({
        credentials: {
          accessKeyId: awsConfig.accessKeyId,
          secretAccessKey: awsConfig.secretAccessKey,
        },
        region: awsConfig.region,
      });

      const command = new DeleteObjectCommand({
        Bucket: awsConfig.bucketName,
        Key: PatientEntity.value,
      });

      await s3.send(command);

      return {
        status: process.env.FILE_DELETE_SUCCESS_RESP,
      };
    } catch (error) {
      return error;
    }
  }
}
