import {
  Controller,
  Post,
  Body,
  UseInterceptors,
  UploadedFile,
} from "@nestjs/common";
import { UploadDocPreviwService } from "./upload-doc-previw.service";
import { FileInterceptor } from "@nestjs/platform-express";
import { awsConfig } from "src/aws.config";
import {
  S3,
  GetObjectCommand,
  S3Client,
  PutObjectCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

@Controller("upload-doc-previw")
export class UploadDocPreviwController {
  constructor(
    private readonly uploadDocPreviwService: UploadDocPreviwService
  ) { }

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

        const data = await s3.send(command);
        console.log(data, "data");
        return await this.uploadDocPreviwService.create(
          dongri,
          opd_id,
          hospital_id,
          abhaAddress
        );
      }
    } catch (error) {
      console.error(error);
      let response = {
        status: "failed",
        message: "file upload failed",
      };
      return response;
    }
  }

  @Post("/get")
  async findAll(@Body("value") value: any) {
    try {
      const s3 = new S3({
        credentials: {
          accessKeyId: awsConfig.accessKeyId,
          secretAccessKey: awsConfig.secretAccessKey,
        },
        region: awsConfig.region,
      });
      const s3Client = new S3Client({
        credentials: {
          accessKeyId: awsConfig.accessKeyId,
          secretAccessKey: awsConfig.secretAccessKey,
        },
        region: awsConfig.region,
      });
      const command = new GetObjectCommand({
        Bucket: awsConfig.bucketName,
        Key: value,
      });

      const signedUrl = await getSignedUrl(s3Client, command, {
        expiresIn: 3600,
      });

      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;
    }
  }
  @Post("/get-signed-url")
  async getUrl(@Body("value") value: any) {
    try {
      const s3Client = new S3Client({
        credentials: {
          accessKeyId: awsConfig.accessKeyId,
          secretAccessKey: awsConfig.secretAccessKey,
        },
        region: awsConfig.region,
      });
      const command = new GetObjectCommand({
        Bucket: awsConfig.bucketName,
        Key: value,
      });

      const signedUrl = await getSignedUrl(s3Client, command, {
        expiresIn: 3600,
      });

      return {
        status: "success",
        message: "Image url can be accessed for 1 hour",
        imageURL: signedUrl,
      };
    } catch (error) {
      console.error(error);
      return error;
    }
  }
  @Post("/uploadCaseSheet")
  async uploadCaseSheet(
    @Body('file') file: string,
    @Body("opd_id") opd_id: any,
    @Body("hospital_id") hospital_id: any) {
    return await this.uploadDocPreviwService.uploadDoc(file, opd_id, hospital_id);
  }
}
