package com.hpay.hpay_mobile_api.utils;


import jakarta.mail.*;
import jakarta.mail.internet.*;
import java.util.*;


public class SendEmailAddCard {

    final static String HOST = "mail.hpaytest.cash"; // Sender's email address
    final static String FROM = "hpaytest@hpaytest.cash"; //"no-reply@hpaytest.cash"; ;
    final static String PASSWORD = "kOyWT(6NF#jg@"; //"onanajunior92@"; //;  //
    final static String PORT = "465";

    // Send email method
    public static void send(String to, String prenoms, String cardNum, String date, String time) throws MessagingException {

        // Setup the SMTP server properties
        Properties properties = System.getProperties();
        properties.put("mail.smtp.host", HOST);
        properties.put("mail.smtp.port", PORT);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");



        // Create a session with authentication
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM, PASSWORD );  // Use App Password for Gmail if 2FA is enabled
            }
        });

        // Create a new email message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(FROM));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("Nouvelle carte HPay ajoutée à votre compte");

        String htmlContent = "<!DOCTYPE html>" +
                "<html lang='en'>" +
                "<head>" +
                "<meta charset='UTF-8'>" +
                "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                "<title> Nouvelle Carte ajouté </title>" +
                "<style>" +
                "body { font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; }" +
                ".email-container { width: 100%; max-width: 600px; margin: 0 auto; background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }" +
                ".email-header { background-color: #f4511e; color: white; padding: 20px; text-align: center; }" +
                ".email-body { padding: 30px; color: #333333; }" +
                ".validation-code { font-size: 24px; font-weight: bold; color: #f4511e; text-align: center; margin-top: 20px; }" +
                ".footer { padding: 20px; background-color: #f1f1f1; text-align: center; font-size: 12px; color: #777777; }" +
                "a { color: #4CAF50; text-decoration: none; }" +
                "</style>" +
                "</head>" +
                "<body>" +
                "<div class='email-container'>" +
                "<div class='email-header'>" +
                "<h1>Nouvelle carte sjoutée</h1>" +
                "</div>" +
                "<div class='email-body'>" +
                "<p>Salut [User's Name],</p>" +
                "<p>Une nouvelle carte HPay portant le numéro [numcarte] a été liée avec succès à votre compte le [date] à [heure].</p>" +
                "<p>Si vous n'êtes pas à l'origine de cette opération, veuillez contacter notre support.</p>" +
                "<p>L'équipe HPay</p>" +
                "</div>" +
                "<div class='footer'>" +
                "<p>&copy; 2025 [Your Company Name]. All rights reserved.</p>" +
                "</div>" +
                "</div>" +
                "</body>" +
                "</html>";

        // Replace placeholders with actual values
        htmlContent = htmlContent.replace("[User's Name]", prenoms);
        htmlContent = htmlContent.replace("[numcarte]", cardNum);
        htmlContent = htmlContent.replace("[date]", date);
        htmlContent = htmlContent.replace("[heure]", time);
        htmlContent = htmlContent.replace("[Your Company Name]", "HeySeller Pay");

        // Set the HTML content to the email
        message.setContent(htmlContent, "text/html");


        // Send the email
        Transport.send(message);
        System.out.println("Email sent successfully to " + to);
    }




}
