class CardTransactionService {

    public ResponseObject<Xs2aCardTransactionsReport> getCardTransactionsReportByPeriod(Xs2aTransactionsReportByPeriodRequest request) {
        xs2aEventService.recordConsentTppRequest(request.getConsentId(), EventType.READ_CARD_TRANSACTION_LIST_REQUEST_RECEIVED);

        Optional<AisConsent> aisConsentOptional = aisConsentService.getAccountConsentById(request.getConsentId());

        if (aisConsentOptional.isEmpty()) {
            log.info("Account-ID [{}], Consent-ID [{}]. Get card transactions report by period failed. Account consent not found by ID",
                     request.getAccountId(), request.getConsentId());
            return ResponseObject.<Xs2aCardTransactionsReport>builder()
                       .fail(AIS_400, TppMessageInformation.of(CONSENT_UNKNOWN_400))
                       .build();
        }

        AisConsent aisConsent = aisConsentOptional.get();
        ValidationResult validationResult = getValidationResultForTransactionsReportByPeriod(request, aisConsent);

        if (validationResult.isNotValid()) {
            log.info("Account-ID [{}], Consent-ID [{}], WithBalance [{}], RequestUri [{}]. Get transactions report by period - validation failed: {}",
                     request.getAccountId(), request.getConsentId(), request.isWithBalance(),
                     request.getRequestUri(), validationResult.getMessageError());
            return ResponseObject.<Xs2aCardTransactionsReport>builder()
                       .fail(validationResult.getMessageError())
                       .build();
        }

        SpiResponse<SpiCardTransactionReport> spiResponse = getSpiResponseSpiCardTransactionReport(request, aisConsent);

        if (spiResponse.hasError()) {
            return checkSpiResponseForCardTransactionsReport(request, spiResponse);
        }

        loggingContextService.storeConsentStatus(aisConsent.getConsentStatus());

        xs2aAccountService.saveNumberOfTransaction(request.getConsentId(), request.getAccountId(), spiResponse.getPayload().getCardTransactions().size());

        return getXs2aCardTransactionsReportResponseObject(request, aisConsent, spiResponse.getPayload());
    }

}