package com.iqser.red.service.persistence.management.v1.processor.configuration;

import static com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingQueueNames.LAYOUT_PARSING_DLQ;

import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingQueueNames;
import com.knecon.fforesight.llm.service.QueueNames;

import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class MessagingConfiguration {

    // persistence-service
    public static final String DOWNLOAD_REQUEST_QUEUE_PREFIX = "download_request";
    public static final String DOWNLOAD_REQUEST_EXCHANGE = "download_request_exchange";
    public static final String DOWNLOAD_DLQ = "download_error";

    public static final String DOWNLOAD_COMPRESSION_REQUEST_QUEUE_PREFIX = "download_compression_request";
    public static final String DOWNLOAD_COMPRESSION_REQUEST_EXCHANGE = "download_compression_request_exchange";
    public static final String DOWNLOAD_COMPRESSION_DLQ = "download_compression_error";

    public static final String EXPORT_DOWNLOAD_REQUEST_QUEUE_PREFIX = "export_download_request";
    public static final String EXPORT_DOWNLOAD_REQUEST_EXCHANGE = "export_download_request_exchange";
    public static final String EXPORT_DOWNLOAD_DLQ = "export_download_error";

    public static final String ANALYSIS_FLAG_CALCULATION_REQUEST_QUEUE_PREFIX = "analysis_flag_calculation_request";
    public static final String ANALYSIS_FLAG_CALCULATION_REQUEST_EXCHANGE = "analysis_flag_calculation_request_exchange";

    public static final String REDACTION_RESPONSE_QUEUE_PREFIX = "redaction_response";
    public static final String REDACTION_RESPONSE_EXCHANGE = "redaction_response_exchange";

    public static final String SEARCH_BULK_LOCAL_TERM_RESPONSE_QUEUE_PREFIX = "search_bulk_local_term_response";
    public static final String SEARCH_BULK_LOCAL_TERM_RESPONSE_EXCHANGE = "search_bulk_local_term_response_exchange";
    public static final String SEARCH_BULK_LOCAL_TERM_DLQ = "search_bulk_local_term_error";

    public static final String PDFTRON_RESPONSE_QUEUE_PREFIX = "pdftron_response";
    public static final String PDFTRON_RESPONSE_EXCHANGE = "pdftron_response_exchange";

    public static final String REPORT_RESPONSE_QUEUE_PREFIX = "report_response";
    public static final String REPORT_RESPONSE_EXCHANGE = "report_response_exchange";
    public static final String REPORT_RESPONSE_DLQ = "report_response_error";

    public static final String OCR_RESPONSE_QUEUE_PREFIX = "ocr_response";
    public static final String OCR_RESPONSE_EXCHANGE = "ocr_response_exchange";
    public static final String OCR_STATUS_UPDATE_RESPONSE_QUEUE_PREFIX = "ocr_status_update_response";
    public static final String OCR_STATUS_UPDATE_RESPONSE_EXCHANGE = "ocr_status_update_response_exchange";
    public static final String OCR_STATUS_UPDATE_DLQ = "ocr_status_update_error";

    public static final String IMAGE_RESPONSE_QUEUE_PREFIX = "image_response";
    public static final String IMAGE_RESPONSE_EXCHANGE = "image_response_exchange";

    public static final String ENTITY_RESPONSE_QUEUE_PREFIX = "entity_response";
    public static final String ENTITY_RESPONSE_EXCHANGE = "entity_response_exchange";

    public static final String AZURE_ENTITY_RESPONSE_QUEUE_PREFIX = "azure_entity_response";
    public static final String AZURE_ENTITY_RESPONSE_EXCHANGE = "azure_entity_response_exchange";

    public static final String CHUNKING_RESPONSE_QUEUE_PREFIX = "chunking_response";
    public static final String CHUNKING_RESPONSE_EXCHANGE = "chunking_response_exchange";
    public static final String CHUNKING_DLQ = "chunking_error";

    public static final String CV_ANALYSIS_RESPONSE_QUEUE_PREFIX = "cv_analysis_response";
    public static final String CV_ANALYSIS_RESPONSE_EXCHANGE = "cv_analysis_response_exchange";

    public static final String VISUAL_LAYOUT_PARSING_RESPONSE_QUEUE_PREFIX = "visual_layout_parsing_response";
    public static final String VISUAL_LAYOUT_PARSING_RESPONSE_EXCHANGE = "visual_layout_parsing_response_exchange";

    // pdftron-redaction-service
    public static final String PREPROCESSING_REQUEST_EXCHANGE = "preprocessing_request_exchange";
    public static final String PREPROCESSING_DLQ = "preprocessing_error";

    public static final String PDFTRON_REQUEST_EXCHANGE = "pdftron_request_exchange";
    public static final String PDFTRON_DLQ = "pdftron_error";

    // redaction-service
    public static final String REDACTION_REQUEST_QUEUE_PREFIX = "redaction_request";
    public static final String REDACTION_REQUEST_EXCHANGE = "redaction_request_exchange";
    public static final String REDACTION_PRIORITY_REQUEST_QUEUE_PREFIX = "redaction_priority_request";
    public static final String REDACTION_PRIORITY_REQUEST_EXCHANGE = "redaction_priority_request_exchange";
    public static final String REDACTION_DLQ = "redaction_error";

    // redaction-report-service
    public static final String REPORT_REQUEST_EXCHANGE = "report_request_exchange";
    public static final String REPORT_REQUEST_DLQ = "report_request_error";

    // search-service
    public static final String INDEXING_REQUEST_EXCHANGE = "indexing_request_exchange";
    public static final String INDEXING_DLQ = "indexing_error";

    public static final String DELETE_FROM_INDEX_REQUEST_EXCHANGE = "delete_from_index_request_exchange";
    public static final String DELETE_FROM_INDEX_DLQ = "delete_from_index_error";

    // ocr-services
    public static final String OCR_REQUEST_EXCHANGE = "ocr_request_exchange";
    public static final String OCR_DLQ = "ocr_error";

    // image-service
    public static final String IMAGE_REQUEST_EXCHANGE = "image_request_exchange";
    public static final String IMAGE_DLQ = "image_error";

    // entity-recognition-service
    public static final String ENTITY_REQUEST_EXCHANGE = "entity_request_exchange";
    public static final String ENTITY_DLQ = "entity_error";

    // azure-ner-service
    public static final String AZURE_ENTITY_REQUEST_EXCHANGE = "azure_entity_request_exchange";
    public static final String AZURE_ENTITY_DLQ = "azure_entity_error";

    // cv-analysis-service
    public static final String CV_ANALYSIS_REQUEST_EXCHANGE = "cv_analysis_request_exchange";
    public static final String CV_ANALYSIS_DLQ = "cv_analysis_error";

    // visual-layout-parsing-service
    public static final String VISUAL_LAYOUT_PARSING_REQUEST_EXCHANGE = "visual_layout_parsing_request_exchange";
    public static final String VISUAL_LAYOUT_PARSING_DLQ = "visual_layout_parsing_error";

    //chunking-service
    public static final String CHUNKING_REQUEST_EXCHANGE = "chunking_request_exchange";

    //llm-ner-service
    public static final String LLM_NER_REQUEST_EXCHANGE = QueueNames.LLM_NER_REQUEST_EXCHANGE;
    public static final String LLM_NER_RESPONSE_QUEUE_PREFIX = QueueNames.LLM_NER_RESPONSE_QUEUE_PREFIX;
    public static final String LLM_NER_RESPONSE_EXCHANGE = QueueNames.LLM_NER_RESPONSE_EXCHANGE;
    public static final String LLM_NER_DLQ = QueueNames.LLM_NER_DLQ;

    public static final String X_ERROR_INFO_HEADER = "x-error-message";
    public static final String X_ERROR_INFO_TIMESTAMP_HEADER = "x-error-message-timestamp";

    // ---- Saas Migration, can be removed later ----
    public static final String MIGRATION_REQUEST_QUEUE = "migrationQueue";
    public static final String MIGRATION_RESPONSE_QUEUE = "migrationResponseQueue";
    public static final String MIGRATION_DLQ = "migrationDLQ";


    // ---- persistence-service ----
    @Bean
    public DirectExchange downloadExchange() {

        return new DirectExchange(DOWNLOAD_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue downloadDeadLetterQueue() {

        return QueueBuilder.durable(DOWNLOAD_DLQ).build();
    }


    @Bean
    public DirectExchange downloadCompressionExchange() {

        return new DirectExchange(DOWNLOAD_COMPRESSION_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue downloadCompressionQueueDLQ() {

        return QueueBuilder.durable(DOWNLOAD_COMPRESSION_DLQ).build();
    }


    @Bean
    public DirectExchange exportDownloadExchange() {

        return new DirectExchange(EXPORT_DOWNLOAD_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue exportDownloadDeadLetterQueue() {

        return QueueBuilder.durable(EXPORT_DOWNLOAD_DLQ).build();
    }


    @Bean
    public DirectExchange analysisFlagCalculationExchange() {

        return new DirectExchange(ANALYSIS_FLAG_CALCULATION_REQUEST_EXCHANGE);
    }


    @Bean
    public DirectExchange redactionResponseExchange() {

        return new DirectExchange(REDACTION_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange searchTermOccurrencesResponseExchange() {

        return new DirectExchange(SEARCH_BULK_LOCAL_TERM_RESPONSE_EXCHANGE);
    }


    @Bean
    public Queue searchTermOccurrencesDLQ() {

        return QueueBuilder.durable(SEARCH_BULK_LOCAL_TERM_DLQ).build();
    }


    @Bean
    public DirectExchange reportResponseExchange() {

        return new DirectExchange(REPORT_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange chunkingResponseExchange() {

        return new DirectExchange(CHUNKING_RESPONSE_EXCHANGE);
    }


    @Bean
    public Queue chunkingDLQ() {

        return QueueBuilder.durable(CHUNKING_DLQ).build();
    }


    @Bean
    public Queue reportResponseDLQ() {

        return QueueBuilder.durable(REPORT_RESPONSE_DLQ).build();
    }


    @Bean
    public DirectExchange ocrResponseExchange() {

        return new DirectExchange(OCR_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange imageResponseExchange() {

        return new DirectExchange(IMAGE_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange entityResponseExchange() {

        return new DirectExchange(ENTITY_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange azureEntityResponseExchange() {

        return new DirectExchange(AZURE_ENTITY_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange cvAnalysisResponseExchange() {

        return new DirectExchange(CV_ANALYSIS_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange visualLayoutParsingResponseExchange() {

        return new DirectExchange(VISUAL_LAYOUT_PARSING_RESPONSE_EXCHANGE);
    }


    @Bean
    public DirectExchange layoutParsingResponseExchange() {

        return new DirectExchange(LayoutParsingQueueNames.LAYOUT_PARSING_RESPONSE_EXCHANGE);
    }


    // ---- pdftron-redaction-service ----
    @Bean
    public DirectExchange preprocessingRequestExchange() {

        return new DirectExchange(PREPROCESSING_REQUEST_EXCHANGE);
    }


    @Bean
    public DirectExchange preprocessingResponseExchange() {

        return new DirectExchange(PREPROCESSING_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue preprocessingDLQ() {

        return QueueBuilder.durable(PREPROCESSING_DLQ).build();
    }


    @Bean
    public DirectExchange pdftronRequestExchange() {

        return new DirectExchange(PDFTRON_REQUEST_EXCHANGE);
    }


    @Bean
    public DirectExchange pdftronResponseExchange() {

        return new DirectExchange(PDFTRON_RESPONSE_EXCHANGE);
    }


    @Bean
    public Queue pdfTronDLQ() {

        return QueueBuilder.durable(PDFTRON_DLQ).build();
    }


    // ---- redaction-service ----
    @Bean
    public DirectExchange redactionRequestExchange() {

        return new DirectExchange(REDACTION_REQUEST_EXCHANGE);
    }


    @Bean
    public DirectExchange redactionPriorityRequestExchange() {

        return new DirectExchange(REDACTION_PRIORITY_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue redactionDLQ() {

        return QueueBuilder.durable(REDACTION_DLQ).build();
    }


    // ---- redaction-report-service ----
    @Bean
    public DirectExchange reportRequestExchange() {

        return new DirectExchange(REPORT_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue reportRequestDLQ() {

        return QueueBuilder.durable(REPORT_REQUEST_DLQ).build();
    }


    // ---- search-service ----
    @Bean
    public DirectExchange indexingRequestExchange() {

        return new DirectExchange(INDEXING_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue indexingDLQ() {

        return QueueBuilder.durable(INDEXING_DLQ).build();
    }


    @Bean
    public DirectExchange deleteFromIndexRequestExchange() {

        return new DirectExchange(DELETE_FROM_INDEX_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue deleteFromIndexDLQ() {

        return QueueBuilder.durable(DELETE_FROM_INDEX_DLQ).build();
    }


    // ---- ocr-services ----
    @Bean
    public DirectExchange ocrRequestExchange() {

        return new DirectExchange(OCR_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue ocrDLQ() {

        return QueueBuilder.durable(OCR_DLQ).build();
    }


    @Bean
    public DirectExchange ocrStatusUpdateResponseExchange() {

        return new DirectExchange(OCR_STATUS_UPDATE_RESPONSE_EXCHANGE);
    }


    @Bean
    public Queue ocrStatusUpdateDLQ() {

        return QueueBuilder.durable(OCR_STATUS_UPDATE_DLQ).build();
    }


    // ---- image-service ----
    @Bean
    public DirectExchange imageRequestExchange() {

        return new DirectExchange(IMAGE_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue imageDLQ() {

        return QueueBuilder.durable(IMAGE_DLQ).build();
    }


    // ---- entity-recognition-service ----
    @Bean
    public DirectExchange nerRequestExchange() {

        return new DirectExchange(ENTITY_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue nerDLQ() {

        return QueueBuilder.durable(ENTITY_DLQ).build();
    }


    // ---- azure-ner-service ----
    @Bean
    public DirectExchange azureNerRequestExchange() {

        return new DirectExchange(AZURE_ENTITY_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue azureNerDLQ() {

        return QueueBuilder.durable(AZURE_ENTITY_DLQ).build();
    }


    // ---- cv-analysis-service ----
    @Bean
    public DirectExchange cvAnalysisRequestExchange() {

        return new DirectExchange(CV_ANALYSIS_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue cvAnalysisDLQ() {

        return QueueBuilder.durable(CV_ANALYSIS_DLQ).build();
    }


    // ---- visual-layout-parsing-service ----
    @Bean
    public DirectExchange visualLayoutParsingRequestExchange() {

        return new DirectExchange(VISUAL_LAYOUT_PARSING_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue visualLayoutParsingDLQ() {

        return QueueBuilder.durable(VISUAL_LAYOUT_PARSING_DLQ).build();
    }


    // ---- layoutparser-service ----
    @Bean
    public DirectExchange layoutParsingRequestExchange() {

        return new DirectExchange(LayoutParsingQueueNames.LAYOUT_PARSING_REQUEST_EXCHANGE);
    }


    @Bean
    public Queue layoutParsingDLQ() {

        return QueueBuilder.durable(LAYOUT_PARSING_DLQ).build();
    }


    // ---- chunking-service ----
    @Bean
    public DirectExchange chunkingRequestExchange() {

        return new DirectExchange(CHUNKING_REQUEST_EXCHANGE);
    }


    // ----- llm-ner-service ----
    @Bean
    public DirectExchange llmNerRequestExchange() {

        return new DirectExchange(LLM_NER_REQUEST_EXCHANGE);
    }


    @Bean
    public DirectExchange llmNerResponseExchange() {

        return new DirectExchange(LLM_NER_RESPONSE_EXCHANGE);
    }


    @Bean
    public Queue llmNerDLQ() {

        return QueueBuilder.durable(LLM_NER_DLQ).build();
    }


    // ---- Saas Migration ----
    @Bean
    public Queue migrationQueue() {

        return QueueBuilder.durable(MIGRATION_REQUEST_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", MIGRATION_DLQ)
                .maxPriority(2)
                .build();
    }


    @Bean
    public Queue migrationDLQ() {

        return QueueBuilder.durable(MIGRATION_DLQ).build();
    }


    @Bean
    public Queue migrationResponseQueue() {

        return QueueBuilder.durable(MIGRATION_RESPONSE_QUEUE)
                .withArgument("x-dead-letter-exchange", "")
                .withArgument("x-dead-letter-routing-key", MIGRATION_DLQ)
                .maxPriority(2)
                .build();
    }

}
