package at.procon.dip.ingestion.config;

import at.procon.dip.domain.access.DocumentVisibility;
import at.procon.dip.ingestion.mail.MailServerProtocol;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "dip.ingestion")
@Data
public class DipIngestionProperties {

    private boolean enabled = false;
    private boolean fileSystemEnabled = false;
    private boolean restUploadEnabled = true;
    private String inputDirectory = "/ted.europe/generic-input";
    private String filePattern = ".*\\.(pdf|txt|html|htm|xml|md|markdown|csv|json|yaml|yml)$";
    private String processedDirectory = ".dip-processed";
    private String errorDirectory = ".dip-error";

    @Positive
    private long pollInterval = 15000;

    @Positive
    private int maxMessagesPerPoll = 10;

    private String defaultOwnerTenantKey;
    private DocumentVisibility defaultVisibility = DocumentVisibility.PUBLIC;
    private String defaultLanguageCode;

    private boolean storeOriginalBinaryInDb = true;

    @Positive
    private int maxBinaryBytesInDb = 5242880;

    private boolean deduplicateByContentHash = true;
    private boolean storeOriginalContentForWrapperDocuments = true;
    private boolean vectorizePrimaryRepresentationOnly = true;

    @NotBlank
    private String importBatchId = "phase4-generic";

    private boolean tedPackageAdapterEnabled = true;
    private boolean mailAdapterEnabled = false;
    private MailRouteProperties mailRoute = new MailRouteProperties();

    private String mailDefaultOwnerTenantKey;
    private DocumentVisibility mailDefaultVisibility = DocumentVisibility.TENANT;
    private boolean expandMailZipAttachments = true;

    @NotBlank
    private String tedPackageImportBatchId = "phase41-ted-package";

    /**
     * Skip synchronous lexical tsvector indexing during TED package child import.
     * Missing search vectors are backfilled asynchronously by the lexical index scheduler.
     */
    private boolean tedPackageDeferLexicalIndexing = true;

    /**
     * Number of worker threads used for TED package child imports.
     * Set to 1 to keep child processing sequential.
     */
    @Positive
    private int tedPackageChildParallelism = 1;

    /**
     * Maximum number of TED package child tasks allowed to be in flight (running or queued)
     * while streaming a package. This provides bounded backpressure and avoids buffering the
     * whole package in memory before processing starts.
     */
    @Positive
    private int tedPackageChildMaxInFlight = 8;

    private boolean gatewayOnlyForTedPackages = false;

    @NotBlank
    private String mailImportBatchId = "phase41-mail";

    @Data
    public static class MailRouteProperties {
        private boolean enabled = false;
        private MailServerProtocol protocol = MailServerProtocol.IMAPS;
        private String host;
        private Integer port;
        private String username;
        private String password;
        private String folderName = "INBOX";
        private String accountKey;
        private boolean delete = false;
        private boolean ssl = true;
        /**
         * true = fetch only unseen messages
         */
        private boolean unseen = true;
        /**
         * true = do not mark messages as seen while consuming
         */
        private boolean peek = true;
        @Positive
        private long delay = 15000;
        @Positive
        private int maxMessagesPerPoll = 20;
        private int fetchSize = -1;
        private boolean closeFolder = false;
        private boolean debugMode = false;
        private int connectionTimeout = 30000;
        private int consumerConnectionTimeout = 30000;
    }
}
