package com.iqser.red.service.persistence.service.v1.api.external.resource;

import java.util.List;
import java.util.Set;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.iqser.red.service.persistence.service.v1.api.shared.model.PageExclusionRequest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

@ResponseStatus(value = HttpStatus.NO_CONTENT)
public interface ReanalysisResource {

    String TOGGLE_ANALYSIS_PATH = ExternalApi.BASE_PATH + "/toggle-analysis";
    String TOGGLE_AUTOMATIC_ANALYSIS_PATH = ExternalApi.BASE_PATH + "/toggle-automatic-analysis";
    String REANALYSIS_REST_PATH = ExternalApi.BASE_PATH + "/reanalyze";
    String REINDEX_REST_PATH = ExternalApi.BASE_PATH + "/reindex";
    String OCR_REANALYSIS_REST_PATH = ExternalApi.BASE_PATH + "/ocr/reanalyze";
    String EXCLUDE_PAGES_PATH = ExternalApi.BASE_PATH + "/exclude-pages";
    String INCLUDE_PAGES_PATH = ExternalApi.BASE_PATH + "/include-pages";

    String BULK_REST_PATH = "/bulk";

    String DOSSIER_ID = "dossierId";
    String FILE_ID = "fileId";

    String DOSSIER_ID_PATH_VARIABLE = "/{" + DOSSIER_ID + "}";
    String FILE_ID_PATH_VARIABLE = "/{" + FILE_ID + "}";
    String FALSE = "false";

    String EXCLUDED_STATUS_PARAM = "excluded";
    String FORCE_PARAM = "force";


    @PostMapping(value = REANALYSIS_REST_PATH + DOSSIER_ID_PATH_VARIABLE)
    @Operation(summary = "Reanalyze all files of the dossier.", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    void reanalyzeDossier(@PathVariable(DOSSIER_ID) String dossierId, @RequestParam(value = FORCE_PARAM, required = false, defaultValue = FALSE) boolean force);


    @PostMapping(value = REANALYSIS_REST_PATH + DOSSIER_ID_PATH_VARIABLE + FILE_ID_PATH_VARIABLE)
    @Operation(summary = "Reanalyze a file", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    void reanalyzeFile(@PathVariable(DOSSIER_ID) String dossierId,
                       @PathVariable(FILE_ID) String fileId,
                       @RequestParam(value = FORCE_PARAM, required = false, defaultValue = FALSE) boolean force);


    @PostMapping(value = REANALYSIS_REST_PATH + DOSSIER_ID_PATH_VARIABLE + BULK_REST_PATH)
    @Operation(summary = "Reanalyze multiple files for a dossier", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    void reanalyzeFilesForDossier(@PathVariable(DOSSIER_ID) String dossierId,
                                  @RequestBody List<String> fileIds,
                                  @RequestParam(value = FORCE_PARAM, required = false, defaultValue = FALSE) boolean force);


    @Operation(summary = "Ocr and reanalyze a dossier", description = "None")
    @PostMapping(value = OCR_REANALYSIS_REST_PATH + DOSSIER_ID_PATH_VARIABLE)
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    void ocrDossier(@PathVariable(DOSSIER_ID) String dossierId);


    @Operation(summary = "Ocr and reanalyze a file", description = "None")
    @PostMapping(value = OCR_REANALYSIS_REST_PATH + DOSSIER_ID_PATH_VARIABLE + FILE_ID_PATH_VARIABLE)
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK"), @ApiResponse(responseCode = "409", description = "Conflict")})
    void ocrFile(@PathVariable(DOSSIER_ID) String dossierId,
                 @PathVariable(FILE_ID) String fileId,
                 @RequestParam(value = FORCE_PARAM, required = false, defaultValue = FALSE) boolean force);


    @Operation(summary = "Ocr and reanalyze multiple files for a dossier", description = "None")
    @PostMapping(value = OCR_REANALYSIS_REST_PATH + DOSSIER_ID_PATH_VARIABLE + BULK_REST_PATH)
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    void ocrFiles(@PathVariable(DOSSIER_ID) String dossierId, @RequestBody Set<String> fileIds);


    @Operation(summary = "Exclude or re-include a file to the automatic analysis", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    @PostMapping(value = TOGGLE_AUTOMATIC_ANALYSIS_PATH + DOSSIER_ID_PATH_VARIABLE + FILE_ID_PATH_VARIABLE)
    void toggleAutomaticAnalysis(@PathVariable(DOSSIER_ID) String dossierId,
                                 @PathVariable(FILE_ID) String fileId,
                                 @RequestParam(EXCLUDED_STATUS_PARAM) boolean excludedFromAutomaticAnalysis);


    @Operation(summary = "Exclude or re-include a file to analysis", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})

    @PostMapping(value = TOGGLE_ANALYSIS_PATH + DOSSIER_ID_PATH_VARIABLE + FILE_ID_PATH_VARIABLE)
    void toggleExclusion(@PathVariable(DOSSIER_ID) String dossierId,
                         @PathVariable(FILE_ID) String fileId,
                         @RequestParam(name = EXCLUDED_STATUS_PARAM, required = false, defaultValue = FALSE) boolean excluded);


    @Operation(summary = "Exclude or re-include a file to the automatic analysis", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})

    @PostMapping(value = TOGGLE_AUTOMATIC_ANALYSIS_PATH + DOSSIER_ID_PATH_VARIABLE + BULK_REST_PATH)
    void toggleAutomaticAnalysisForList(@PathVariable(DOSSIER_ID) String dossierId,
                                        @RequestBody Set<String> fileIds,
                                        @RequestParam(EXCLUDED_STATUS_PARAM) boolean excludedFromAutomaticAnalysis);


    @Operation(summary = "Exclude or re-include a file to analysis", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})

    @PostMapping(value = TOGGLE_ANALYSIS_PATH + DOSSIER_ID_PATH_VARIABLE + BULK_REST_PATH)
    void toggleExclusionForList(@PathVariable(DOSSIER_ID) String dossierId,
                                @RequestBody Set<String> fileIds,
                                @RequestParam(name = EXCLUDED_STATUS_PARAM, required = false, defaultValue = FALSE) boolean excluded);


    @Operation(summary = "Exclude pages from analysis for a file", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})

    @PostMapping(value = EXCLUDE_PAGES_PATH + DOSSIER_ID_PATH_VARIABLE + FILE_ID_PATH_VARIABLE)
    void excludePages(@PathVariable(DOSSIER_ID) String dossierId, @PathVariable(FILE_ID) String fileId, @RequestBody PageExclusionRequest pageExclusionRequest);


    @Operation(summary = "Include pages from analysis for a file", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    @PostMapping(value = INCLUDE_PAGES_PATH + DOSSIER_ID_PATH_VARIABLE + FILE_ID_PATH_VARIABLE)
    void includePages(@PathVariable(DOSSIER_ID) String dossierId, @PathVariable(FILE_ID) String fileId, @RequestBody PageExclusionRequest pageInclusionRequest);


    @PostMapping(value = REINDEX_REST_PATH)
    @Operation(summary = "Reindex a dossier, files of a dossier or all", description = "None")
    @ApiResponses(value = {@ApiResponse(responseCode = "204", description = "OK")})
    void reindex(@RequestParam(value = "dossierId", required = false) String dossierId,
                 @RequestParam(value = "dropIndex", required = false, defaultValue = FALSE) boolean dropIndex,
                 @RequestBody List<String> fileIds);

}
