package com.iqser.red.service.persistence.v1.internal.api.controller;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.service.persistence.management.v1.processor.configuration.MessagingConfiguration;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusProcessingUpdateService;
import com.iqser.red.service.persistence.service.v1.api.internal.resources.FileStatusProcessingUpdateResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.AnalyzeResult;
import com.iqser.red.service.persistence.service.v1.api.shared.model.UntouchedDocumentResponse;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileErrorInfo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
public class FileStatusProcessingUpdateInternalController implements FileStatusProcessingUpdateResource {

    private final FileStatusProcessingUpdateService fileStatusProcessingUpdateService;


    public void preprocessingSuccessful(@PathVariable(DOSSIER_ID_PARAM) String dossierId,
                                        @PathVariable(FILE_ID) String fileId,
                                        @RequestBody UntouchedDocumentResponse untouchedDocumentResponse) {

        fileStatusProcessingUpdateService.preprocessingSuccessful(dossierId, fileId, untouchedDocumentResponse);
    }


    public void preprocessingFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.preprocessingFailed(dossierId, fileId, new FileErrorInfo("preprocessing failed",
                MessagingConfiguration.PRE_PROCESSING_DLQ, "pdftron-service", OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)));
    }


    public void preprocessingFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId,
                                    @RequestBody FileErrorInfo fileErrorInfo) {

        fileStatusProcessingUpdateService.preprocessingFailed(dossierId, fileId, fileErrorInfo);
    }


    public void analysisSuccessful(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId, @RequestBody AnalyzeResult analyzeResult) {

        fileStatusProcessingUpdateService.analysisSuccessful(dossierId, fileId, analyzeResult);
    }


    public void ocrSuccessful(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.ocrSuccessful(dossierId, fileId);
    }


    public void analysisFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.analysisFailed(dossierId, fileId, new FileErrorInfo());
    }


    public void analysisFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId,
                               @RequestBody FileErrorInfo fileErrorInfo) {

        fileStatusProcessingUpdateService.analysisFailed(dossierId, fileId, fileErrorInfo);
    }


    public void preProcessing(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.preProcessing(dossierId, fileId);
    }


    public void ocrProcessing(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.ocrProcessing(dossierId, fileId);
    }


    public void ocrFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.ocrFailed(dossierId, fileId, new FileErrorInfo("ocr failed",
                MessagingConfiguration.OCR_DLQ, "ocr-service", OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)));
    }


    public void ocrFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId,
                          @RequestBody FileErrorInfo fileErrorInfo) {

        fileStatusProcessingUpdateService.ocrFailed(dossierId, fileId, fileErrorInfo);
    }


    public void indexing(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.indexing(dossierId, fileId);
    }


    public void indexingSuccessful(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.indexingSuccessful(dossierId, fileId);
    }


    public void indexingFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId) {

        fileStatusProcessingUpdateService.indexingFailed(dossierId, fileId, new FileErrorInfo("indexing failed",
                MessagingConfiguration.INDEXING_DQL, "search-service", OffsetDateTime.now().truncatedTo(ChronoUnit.MILLIS)));
    }

    public void indexingFailed(@PathVariable(DOSSIER_ID_PARAM) String dossierId, @PathVariable(FILE_ID) String fileId,
                               @RequestBody FileErrorInfo fileErrorInfo) {

        fileStatusProcessingUpdateService.indexingFailed(dossierId, fileId, fileErrorInfo);
    }

}
