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

import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierEntity;
import com.iqser.red.service.persistence.management.v1.processor.service.FileManagementStorageService;
import com.iqser.red.service.persistence.management.v1.processor.utils.StorageIdUtils;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingRequest;
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class LayoutParsingRequestFactory {

    @Value("${application.type}")
    private String applicationType;

    private final FileManagementStorageService fileManagementStorageService;
    private final LayoutParsingRequestIdentifierService layoutParsingRequestIdentifierService;


    public LayoutParsingRequest build(String dossierId, String fileId, boolean priority, DossierEntity dossier) {

        LayoutParsingType type = switch (applicationType) {
            case "DocuMine" -> LayoutParsingType.DOCUMINE;
            case "TAAS" -> LayoutParsingType.TAAS;
            default -> LayoutParsingType.REDACT_MANAGER;
        };

        Optional<String> optionalImageFileId = fileManagementStorageService.objectExists(dossierId, fileId, FileType.IMAGE_INFO) //
                ? Optional.of(StorageIdUtils.getStorageId(dossierId, fileId, FileType.IMAGE_INFO)) : Optional.empty();

        Optional<String> optionalTableFileId = fileManagementStorageService.objectExists(dossierId, fileId, FileType.TABLES) //
                ? Optional.of(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TABLES)) : Optional.empty();

        return LayoutParsingRequest.builder()
                .layoutParsingType(type)
                .identifier(layoutParsingRequestIdentifierService.buildIdentifier(dossierId, fileId, priority))
                .originFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.ORIGIN))
                .imagesFileStorageId(optionalImageFileId)
                .tablesFileStorageId(optionalTableFileId)
                .pageFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_PAGES))
                .structureFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_STRUCTURE))
                .textBlockFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_TEXT))
                .positionBlockFileStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.DOCUMENT_POSITION))
                .simplifiedTextStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.SIMPLIFIED_TEXT))
                .sectionGridStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.SECTION_GRID))
                .viewerDocumentStorageId(StorageIdUtils.getStorageId(dossierId, fileId, FileType.VIEWER_DOCUMENT))
                .build();
    }

}
