package creditfair_websiteapp_email;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import creditfair_websiteapp_config.WebsiteappBrowser;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.*;
import software.amazon.awssdk.core.SdkBytes;

public class EmailReport extends WebsiteappBrowser {
<<<<<<< HEAD

	public void emailSend ()
=======
	public void emailSend () throws IOException
>>>>>>> 7cef4cfa541e2f76672f9a27d0f2fe6d4f11be11
	{
		WebsiteappBrowser keypasses =new WebsiteappBrowser();
		
	    String accessKey = keypasses.sesKey();
	    String secretKey = keypasses.sesSecretkey();
	    String region = "ap-south-1";
	    String uploadFile = System.getProperty("user.dir")+"/test-output/emailable-report.html";
	    String subject = "Website Application Created Successfully";
	    String emailBody ="Application create successfully and Deleted";
	    
	    // Replace these values with your email addresses
        String sender = "devteam@creditfair.in";
        String recipient = "pratik@creditfair.in";

        // Create AWS credentials
        AwsBasicCredentials awsCredentials = AwsBasicCredentials.create(accessKey, secretKey);

        // Create SES client with static credentials
        SesClient client = SesClient.builder()
                .region((Region.AP_SOUTH_1))
                .credentialsProvider(StaticCredentialsProvider.create(awsCredentials))
                .build();

        SdkBytes emailData = encodeEmailWithAttachment(sender, recipient, subject, emailBody, uploadFile);
        
        SendRawEmailRequest rawEmailRequest = SendRawEmailRequest.builder()
                .rawMessage(RawMessage.builder().data(emailData).build())
                .build();
          
        // Create a simple message
//        Message message = Message.builder()
//                .subject(Content.builder().data(subject).build())
//                .body(Body.builder().text(Content.builder().data("Application create successfully").build()).build())
//                .build();

        // Create a request
//        SendEmailRequest request = SendEmailRequest.builder()
//                .source(sender)
//                .destination(Destination.builder().toAddresses(recipient).build())
//                .message(message)
//                .build();

        try {
            // Send the email
            SendRawEmailResponse response = client.sendRawEmail(rawEmailRequest);
            System.out.println("Email sent! Message ID: " + response.messageId());
        } catch (SesException e) {
            // Handle exceptions
            e.printStackTrace();
        }
        
    }
	 private static SdkBytes encodeEmailWithAttachment(
	            String sender, String recipient, String subject, String body,String uploadFile) throws IOException {

	        // Create a MIME message with attachment
	        String boundary = "boundary_string";
	        StringBuilder emailContent = new StringBuilder();
	        emailContent.append("From: ").append(sender).append("\r\n");
	        emailContent.append("To: ").append(recipient).append("\r\n");
	        emailContent.append("Subject: ").append(subject).append("\r\n");
	        emailContent.append("Content-type: multipart/mixed; boundary=").append(boundary).append("\r\n\r\n");

	        // Add text part
	        emailContent.append("--").append(boundary).append("\r\n");
	        emailContent.append("Content-type: text/plain\r\n\r\n");
	        emailContent.append(body).append("\r\n\r\n");

	        // Add attachment part
	        Path attachmentPath = Paths.get(uploadFile);
	        byte[] attachmentBytes = Files.readAllBytes(attachmentPath);
	        emailContent.append("--").append(boundary).append("\r\n");
	        emailContent.append("Content-type: application/octet-stream\r\n");
	        emailContent.append("Content-Disposition: attachment; filename=").append(attachmentPath.getFileName()).append("\r\n\r\n");
	        emailContent.append(new String(attachmentBytes)).append("\r\n");

	        // Closing boundary
	        emailContent.append("--").append(boundary).append("--\r\n");

	        return SdkBytes.fromUtf8String(emailContent.toString());
	    }
}


