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


import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_DOSSIER;
import static com.iqser.red.service.persistence.management.v1.processor.service.FeignExceptionHandler.processFeignException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.service.persistence.management.v1.processor.client.redactionreportservice.StatusReportClient;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.service.AccessControlService;
import com.iqser.red.service.persistence.management.v1.processor.utils.StringEncodingUtils;
import com.iqser.red.service.persistence.service.v1.api.external.resource.StatusReportResource;
import com.iqser.red.service.redaction.report.v1.api.model.StatusReportResponse;

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

@Slf4j
@RestController
@RequiredArgsConstructor
public class StatusReportController implements StatusReportResource {

    private final StatusReportClient statusReportClient;
    private final AccessControlService accessControlService;


    @Override
    @PreAuthorize("hasAuthority('" + READ_DOSSIER + "')")
    public ResponseEntity<?> generateStatusReport(@PathVariable(DOSSIER_ID) String dossierId) {

        try {
            accessControlService.verifyUserHasViewPermissions(dossierId);
        } catch (AccessDeniedException e) {
            throw new NotFoundException("Object not found");
        }

        try {
            StatusReportResponse statusReportResponse = statusReportClient.generateStatusReport(dossierId);
            return getResponseEntity(statusReportResponse.getReport(), statusReportResponse.getFilename(), MediaType.APPLICATION_OCTET_STREAM);
        } catch (FeignException e) {
            throw processFeignException(e);
        }

    }


    private ResponseEntity<?> getResponseEntity(byte[] file, String filename, MediaType mediaType) {

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(mediaType);

        if (filename != null) {
            httpHeaders.add("Content-Disposition", "attachment; filename*=utf-8''" + StringEncodingUtils.urlEncode(filename));
        }
        InputStream is = new ByteArrayInputStream(file);

        return new ResponseEntity<>(new InputStreamResource(is), httpHeaders, HttpStatus.OK);

    }

}
