class PaullEndpoint {

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getDetailsPESAllerRequest")
    public @ResponsePayload
    GetDetailsPESAllerResponse getDetailsPESAller(
            @RequestPayload GetDetailsPESAllerRequest getDetailsPESAllerRequest) throws IOException {

        PaullSoapToken paullSoapToken = getToken(getDetailsPESAllerRequest.getSessionId());
        if (paullSoapToken == null)
            return getDetailsPESAllerResponseError("SESSION_INVALID_OR_EXPIRED");

        GenericAccount genericAccount = externalRestService.getGenericAccount(paullSoapToken.getAccountUuid());
        if (!localAuthorityService.localAuthorityGranted(genericAccount, paullSoapToken.getSiren()))
            return getDetailsPESAllerResponseError("LOCALAUTHORITY_NOT_GRANTED");

        GetDetailsPESAllerResponse detailsPESAllerResponse = new GetDetailsPESAllerResponse();
        GetDetailsPESAllerStruct detailsPESAllerStruct = new GetDetailsPESAllerStruct();

        PesAller pesAller = pesAllerService.getByUuid(getDetailsPESAllerRequest.getIdPesAller());
        Optional<LocalAuthority> localAuthority = localAuthorityService.getBySiren(paullSoapToken.getSiren());

        // PES PJ are not sent to signature, don't bother checking something impossible
        if (!pesAller.isPj() && !pesAllerService.isAPesOrmc(pesAller) && pesAller.getSesileClasseurId() != null) {
            Either<HttpStatus, Classeur> sesileResponse = sesileService.getClasseur(localAuthority.get(),
                    pesAller);
            if (sesileResponse.isLeft()) {
                return getDetailsPESAllerResponseError(sesileResponse.getLeft() + ": "
                        + sesileResponse.getLeft().getReasonPhrase());
            }

            Classeur classeur = sesileResponse.get();
            detailsPESAllerStruct.setNomClasseur(classeur.getNom());
            detailsPESAllerStruct.setEtatclasseur(classeur.getStatus().ordinal() + "");
            if (classeur.getCircuit() != null)
                detailsPESAllerStruct.setCircuitClasseur(classeur.getCircuit());
            else
                detailsPESAllerStruct.setCircuitClasseur("");
            classeur.getActions().forEach(action -> {
                GetDetailsPESAllerStruct1 xmlAction = new GetDetailsPESAllerStruct1();
                xmlAction.setDateAction(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(action.getDate()));
                xmlAction.setLibelleAction(action.getAction());
                xmlAction.setNomActeur(action.getUsername());
                detailsPESAllerStruct.getActionsClasseur().add(xmlAction);
            });
        } else {
            detailsPESAllerStruct.setNomClasseur("");
            detailsPESAllerStruct.setEtatclasseur("");
            detailsPESAllerStruct.setCircuitClasseur("");
        }

        JsonNode node = externalRestService.getProfile(pesAller.getProfileUuid());

        detailsPESAllerStruct.setPESPJ(pesAller.isPj() ? "1" : "0");
        detailsPESAllerStruct.setObjet(pesAller.getObjet());
        detailsPESAllerStruct.setUserName(JsonExtractorUtils.extractEmailFromProfile(node));
        detailsPESAllerStruct.setNomDocument(pesAller.getFileName());
        detailsPESAllerStruct.setDateDepot(dateFormatterWithMinutes.format(pesAller.getCreation()));
        detailsPESAllerStruct.setStatutBannette(pesAller.getLastHistoryStatus().name());

        List<PesHistory> fileHistories = pesAllerService.getPesHistoryByTypes(
                getDetailsPESAllerRequest.getIdPesAller(),
                Arrays.asList(StatusType.ACK_RECEIVED, StatusType.NACK_RECEIVED));

        Optional<PesHistory> peshistory = fileHistories.stream().findFirst();

        if (peshistory.isPresent()) {
            detailsPESAllerStruct.setDateAR(dateFormatter.format(peshistory.get().getDate()));
            detailsPESAllerStruct.setDateAnomalie("");
            detailsPESAllerStruct.setMotifAnomalie("");
        } else if (pesAllerService.hasAFatalError(pesAller)) {
            detailsPESAllerStruct.setDateAR("");
            detailsPESAllerStruct.setDateAnomalie(dateFormatterWithMinutes.format(pesAller.getLastHistoryDate()));
            detailsPESAllerStruct.setMotifAnomalie(pesAller.getLastHistoryStatus().name());
        } else {
            detailsPESAllerStruct.setDateAR("");
            detailsPESAllerStruct.setDateAnomalie("");
            detailsPESAllerStruct.setMotifAnomalie("");
        }

        // bunch of no longer used properties, set them all to an empty string
        detailsPESAllerStruct.setMotifPlusAnomalie("");
        detailsPESAllerStruct.setDateDepotBannette("");
        detailsPESAllerStruct.setStatutBannette("");
        detailsPESAllerStruct.setUserNameBannette("");
        detailsPESAllerStruct.setActeurCourant("");

        detailsPESAllerStruct.setMessage("SUCCESS");
        detailsPESAllerResponse.getRetour().add(detailsPESAllerStruct);
        detailsPESAllerResponse.setStatusCode("OK");
        return detailsPESAllerResponse;

    }

    public  PaullEndpoint(PesAllerService pesAllerService, LocalAuthorityService localAuthorityService,
            ExternalRestService externalRestService, PesRetourService pesRetourService, SesileService sesileService);

}