package at.procon.dip.extraction.impl;

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

import at.procon.dip.classification.detector.BasicMimeAndExtensionDocumentTypeDetector;
import at.procon.dip.classification.spi.DetectionResult;
import at.procon.dip.domain.access.DocumentAccessContext;
import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.document.SourceType;
import at.procon.dip.ingestion.spi.OriginalContentStoragePolicy;
import at.procon.dip.ingestion.spi.SourceDescriptor;
import java.time.OffsetDateTime;
import java.util.Map;
import org.junit.jupiter.api.Test;

class BasicMimeAndExtensionDocumentTypeDetectorTest {

    private final at.procon.dip.classification.detector.BasicMimeAndExtensionDocumentTypeDetector detector = new BasicMimeAndExtensionDocumentTypeDetector();

    @Test
    void shouldResolveKnownExcelMimeFromExtensionWhenMailUsesOctetStream() {
        SourceDescriptor source = new SourceDescriptor(
                DocumentAccessContext.publicDocument(),
                SourceType.MAIL,
                "mail-1:attachment:test.xls",
                "mail://message/1",
                "test.xls",
                "application/octet-stream",
                new byte[] {1,2,3},
                null,
                OffsetDateTime.now(),
                OriginalContentStoragePolicy.STORE,
                Map.of()
        );

        DetectionResult result = detector.detect(source);

        assertThat(result.documentType()).isEqualTo(DocumentType.GENERIC_BINARY);
        assertThat(result.mimeType()).isEqualTo("application/vnd.ms-excel");
        assertThat(result.attributes()).containsEntry("detectedExtension", "xls");
        assertThat(result.attributes()).containsEntry("normalizedMediaType", "application/vnd.ms-excel");
    }

    @Test
    void shouldKeepCsvAsTextAndUseCsvMime() {
        SourceDescriptor source = new SourceDescriptor(
                DocumentAccessContext.publicDocument(),
                SourceType.MAIL,
                "mail-1:attachment:data.csv",
                "mail://message/1",
                "data.csv",
                "application/octet-stream",
                "a,b1,2".getBytes(),
                null,
                OffsetDateTime.now(),
                OriginalContentStoragePolicy.STORE,
                Map.of()
        );

        DetectionResult result = detector.detect(source);

        assertThat(result.documentType()).isEqualTo(DocumentType.TEXT);
        assertThat(result.mimeType()).isEqualTo("text/csv");
    }
}
