package com.iqser.red.persistence.service.v1.external.api.impl.controller;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.GET_SIMILAR_IMAGES;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.commons.spring.ErrorMessage;
import com.iqser.red.service.persistence.management.v1.processor.service.ImageSimilarityService;
import com.iqser.red.service.persistence.service.v1.api.external.resource.ImageSimilaritySearchResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.image.ImageSimilaritySearchRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.image.ImageSimilaritySearchResponse;
import com.iqser.red.service.persistence.service.v1.api.shared.mongo.document.ImageDocument;

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

@RestController
@RequiredArgsConstructor
@Slf4j
public class ImageSimilaritySearchController implements ImageSimilaritySearchResource {

    private final ImageSimilarityService imageSimilarityService;


    @SneakyThrows
    @PreAuthorize("hasAuthority('" + GET_SIMILAR_IMAGES + "')")
    public ResponseEntity getSimilarImages(@RequestBody ImageSimilaritySearchRequest imageSimilaritySearchRequest) {

        log.info("received similiar image search request {}", imageSimilaritySearchRequest);
        if (imageSimilaritySearchRequest.getAnnotationId().isEmpty() || (imageSimilaritySearchRequest.getScope().getFileId().isEmpty() && imageSimilaritySearchRequest.getScope()
                .getTemplateId()
                .isEmpty() && imageSimilaritySearchRequest.getScope().getDossierId().isEmpty())) {
            return new ResponseEntity<>(new ErrorMessage(OffsetDateTime.now(), "Required parameter missing"), HttpStatus.BAD_REQUEST);
        }
        List<ImageDocument> similarImages = this.imageSimilarityService.findSimilarImages(imageSimilaritySearchRequest.getAnnotationId(),
                                                                                          imageSimilaritySearchRequest.getDistance(),
                                                                                          imageSimilaritySearchRequest.getScope());
        List<String> similarImagesIds = new ArrayList<>();
        List<Byte> similarImagesThumbnails = new ArrayList<>();
        similarImages.stream()
                .forEach(doc -> {
                    similarImagesIds.add(doc.getImageId());
                    similarImagesThumbnails.add(doc.getThumbnail());
                });
        ImageSimilaritySearchResponse imageSimilaritySearchResponse = new ImageSimilaritySearchResponse(similarImagesIds, similarImagesThumbnails);
        return new ResponseEntity<>(imageSimilaritySearchResponse, HttpStatus.OK);
    }

}
