<commit_before>package ee.tuleva.onboarding.mandate.email;

import com.microtripit.mandrillapp.lutung.MandrillApi;
import com.microtripit.mandrillapp.lutung.model.MandrillApiError;
import com.microtripit.mandrillapp.lutung.view.MandrillMessage;
import com.microtripit.mandrillapp.lutung.view.MandrillMessageStatus;
import ee.tuleva.onboarding.config.MandateEmailConfiguration;
import ee.tuleva.onboarding.user.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class EmailService {

    private final MandateEmailConfiguration mandateEmailConfiguration;

    private MandrillApi mandrillApi;

    @PostConstruct
    private void initialize() {
        mandrillApi = new MandrillApi(mandateEmailConfiguration.getMandrillKey());
    }

    public void send(User user, Long mandateId, byte[] file) {
        MandrillMessage message = new MandrillMessage();
        message.setSubject("Tuleva Liikme Avaldus");
        message.setFromEmail(mandateEmailConfiguration.getFrom());
        message.setFromName("Tuleva Liige");
        message.setHtml(getHtml(user));
        message.setAutoText(true);

        message.setAttachments(getAttachements(file, user, mandateId));
        message.setTo(getRecipients(user));
        message.setPreserveRecipients(true);
        message.setTags(getTags());

        message.setImportant(true);
        message.setTrackClicks(true);

        try {
            log.info("Sending email from {} for member {} {}",
                    mandateEmailConfiguration.getFrom(),
                    user.getFirstName(), user.getLastName()
            );
            MandrillMessageStatus[] messageStatusReports = mandrillApi
                    .messages().send(message, false);

            log.info("Mandrill API response {}", messageStatusReports[0].getStatus()); //FIXME [0]
        } catch (MandrillApiError mandrillApiError) {
            log.error(mandrillApiError.getMandrillErrorAsJson());
            mandrillApiError.printStackTrace();
        } catch (IOException e) {
            log.error(e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    private String getHtml(User user) {
        return "<h1>Tere!</h1><br />Manuses on avaldus Eesti Väärtpaberikeskusele. <br/><br/> " +
                user.getFirstName() + " " + user.getLastName();
    }

    private List<MandrillMessage.Recipient> getRecipients(User user) {
        ArrayList<MandrillMessage.Recipient> recipients = new ArrayList<MandrillMessage.Recipient>();

        //EVK inbox<commit_msg>Correct mandate email text<commit_after>package ee.tuleva.onboarding.mandate.email;

import com.microtripit.mandrillapp.lutung.MandrillApi;
import com.microtripit.mandrillapp.lutung.model.MandrillApiError;
import com.microtripit.mandrillapp.lutung.view.MandrillMessage;
import com.microtripit.mandrillapp.lutung.view.MandrillMessageStatus;
import ee.tuleva.onboarding.config.MandateEmailConfiguration;
import ee.tuleva.onboarding.user.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class EmailService {

    private final MandateEmailConfiguration mandateEmailConfiguration;

    private MandrillApi mandrillApi;

    @PostConstruct
    private void initialize() {
        mandrillApi = new MandrillApi(mandateEmailConfiguration.getMandrillKey());
    }

    public void send(User user, Long mandateId, byte[] file) {
        MandrillMessage message = new MandrillMessage();
        message.setSubject("Tuleva Liikme Avaldus");
        message.setFromEmail(mandateEmailConfiguration.getFrom());
        message.setFromName("Tule<NAME>");
        message.setHtml(getHtml(user));
        message.setAutoText(true);

        message.setAttachments(getAttachements(file, user, mandateId));
        message.setTo(getRecipients(user));
        message.setPreserveRecipients(true);
        message.setTags(getTags());

        message.setImportant(true);
        message.setTrackClicks(true);

        try {
            log.info("Sending email from {} for member {} {}",
                    mandateEmailConfiguration.getFrom(),
                    user.getFirstName(), user.getLastName()
            );
            MandrillMessageStatus[] messageStatusReports = mandrillApi
                    .messages().send(message, false);

            log.info("Mandrill API response {}", messageStatusReports[0].getStatus()); //FIXME [0]
        } catch (MandrillApiError mandrillApiError) {
            log.error(mandrillApiError.getMandrillErrorAsJson());
            mandrillApiError.printStackTrace();
        } catch (IOException e) {
            log.error(e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    private String getHtml(User user) {
        return (new StringBuilder())
                .append("Tere. <br/>")
                .append("Lisatud on minu pensionifondi valiku- ja vahetusavaldused. <br/><br/>")
                .append("Lugupidamisega </br>")
                .append(user.getFirstName()).append(" ").append(user.getLastName())
                .toString();
    }

    private List<MandrillMessage.Recipient> getRecipients(User user) {
        ArrayList<MandrillMessage.Recipient> recipients = new ArrayList<MandrillMessage.Recipient>();

        //EVK inbox
{'fraction_non_alphanumeric': 0.10705050853301155, 'fraction_numerical': 0.0027581451473883815, 'mean_word_length': 3.2787610619469025, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6889458', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1653, 'n_words': 277}