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

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

import java.util.HashSet;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.service.persistence.management.v1.processor.service.AccessControlService;
import com.iqser.red.service.persistence.management.v1.processor.service.AnalysisFlagsCalculationService;
import com.iqser.red.service.persistence.management.v1.processor.service.FileStatusService;
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.ViewedPagesPersistenceService;
import com.knecon.fforesight.databasetenantcommons.providers.utils.MagicConverter;
import com.iqser.red.service.persistence.service.v1.api.external.resource.ViewedPagesResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.ViewedPages;
import com.iqser.red.service.persistence.service.v1.api.shared.model.ViewedPagesRequest;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.ViewedPage;
import com.knecon.fforesight.keycloakcommons.security.KeycloakSecurity;

import feign.FeignException;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class ViewedPagesController implements ViewedPagesResource {

    private final AccessControlService accessControlService;

    private final ViewedPagesPersistenceService viewedPagesPersistenceService;
    private final FileStatusService fileStatusService;
    private final AnalysisFlagsCalculationService analysisFlagsCalculationService;


    @PreAuthorize("hasAuthority('" + MANAGE_VIEWED_PAGES + "')")
    public void addPage(@PathVariable(DOSSIER_ID) String dossierId, @PathVariable(FILE_ID) String fileId, @RequestBody ViewedPagesRequest viewedPagesRequest) {

        accessControlService.verifyUserIsReviewer(dossierId, fileId);
        viewedPagesPersistenceService.insertPage(fileId, KeycloakSecurity.getUserId(), viewedPagesRequest.getPage());
        var file = fileStatusService.getStatus(fileId);
        analysisFlagsCalculationService.calculateFlags(file.getDossierId(), fileId);
    }


    @PreAuthorize("hasAuthority('" + MANAGE_VIEWED_PAGES + "')")
    public void removePage(@PathVariable(DOSSIER_ID) String dossierId, @PathVariable(FILE_ID) String fileId, @PathVariable(PAGE) int page) {

        accessControlService.verifyUserIsReviewer(dossierId, fileId);
        viewedPagesPersistenceService.removePage(fileId, KeycloakSecurity.getUserId(), page);
        var file = fileStatusService.getStatus(fileId);
        analysisFlagsCalculationService.calculateFlags(file.getDossierId(), fileId);

    }


    @PreAuthorize("hasAuthority('" + MANAGE_VIEWED_PAGES + "')")
    public ViewedPages getViewedPages(@PathVariable(DOSSIER_ID) String dossierId, @PathVariable(FILE_ID) String fileId) {

        accessControlService.verifyUserIsReviewer(dossierId, fileId);
        try {
            var pages = MagicConverter.convert(viewedPagesPersistenceService.findViewedPages(fileId, KeycloakSecurity.getUserId()), ViewedPage.class);
            return ViewedPages.builder().pages(new HashSet<>(pages)).build();
        } catch (FeignException e) {
            throw processFeignException(e);
        }
    }

}
