package at.procon.dip.ingestion.mail;

import at.procon.dip.domain.document.entity.MailRecipientType;
import at.procon.dip.ingestion.service.MailMessageExtractionService;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class MailImportIdentityResolverTest {

    private final MailImportIdentityResolver resolver = new MailImportIdentityResolver();

    @Test
    void shouldPreferProviderMessageKeyForRootIdentity() {
        var parsed = new MailMessageExtractionService.ParsedMailMessage(
                "Subject",
                "Subject",
                "Sender",
                "sender@example.com",
                "Sender <sender@example.com>",
                null,
                "<message-id@example.com>",
                null,
                null,
                OffsetDateTime.now(),
                OffsetDateTime.now(),
                "body",
                "",
                List.of(new MailMessageExtractionService.MailRecipient(MailRecipientType.TO, null, "to@example.com", "to@example.com", 0)),
                List.of()
        );
        var envelope = new GenericMailProviderEnvelope(MailProviderType.IMAP, "account-a", "Inbox", "uid-77", null, Map.of());

        String identity = resolver.resolveRootSourceIdentifier(parsed, envelope, "raw".getBytes());

        assertThat(identity).contains("imap").contains("account-a").contains("Inbox").contains("uid-77");
    }

    @Test
    void shouldUsePartPathForAttachmentIdentity() {
        var attachment = new MailMessageExtractionService.MailAttachment(
                "offer.pdf",
                "application/pdf",
                new byte[]{1, 2, 3},
                3l,
                null,
                "0.1.2",
                "ATTACHMENT",
                null,
                1
        );

        String identity = resolver.resolveAttachmentSourceIdentifier("mail:imap:account:Inbox:uid-77", attachment);

        assertThat(identity).endsWith(":part:0.1.2");
    }
}
