@GetMapping("/v2/distributions")
    @OAuthTestable
    @AppsAllowed(property = "vg.hdc.apps.allowed.distributions")
    @ResponseBody
    public ResponseEntity<Distributions> getDistributionsV2(
            @Valid @UserProfileInject UserProfile userProfile,
            @AccountIdsMax @AccountIds @RequestHeader(value = "accountIds", required = false) Set<String> accountIds,
            @NotEmpty @ValidResourceType @RequestParam(value = "resourceType") String resourceType,
            @RequestParam(value = "startDate", required = false) String startDate,
            @NotEmpty @Date(format = DATE_PATTERN_YYYY_MM_DD) @RequestParam(value = "endDate") String endDate) {
        ResponseEntity<Distributions> distributions;
        UserRequest userRequest = divCapGainsBuilder.buildDistributionUserRequest(userProfile.getPoid(), resourceType, startDate, endDate);
        UserAccountValidationDto userAccountValidationDto = accountRetrievalService.validateAccounts(userProfile, accountIds);
        List<UserAccount> userAccounts = userAccountValidationDto.getValidAccounts();
        //get merged expanded account data
        MergedExpandedAccountData mergedExpandedAccountData = mergedExpandedTAAccountsService.getMergedExpandedAccountData(userAccounts);

        if (ResourceType.ACCRUED_DIVIDEND.equals(userRequest.getResourceType())) {
            //get brokerage accrued dividends
            BrokerageAccruedDividend brokerageAccruedDividend = brokerageAccruedDividendService.getBrokerageAccruedDividends(userAccounts, userRequest);

            //get TA accrued dividends
            MutualFundAccruedDividend mutualFundAccruedDividend = mutualFundAccruedDividendService.getMutualFundAccruedDividend(userAccounts, userRequest);

            AccountAccruedDividend accountAccruedDividend = divCapGainsBuilder.buildDistributionAccountAccruedDividend(brokerageAccruedDividend, mutualFundAccruedDividend);
            distributions = accruedDividendResponseMapper.mapResults(accountAccruedDividend, userAccounts, userAccountValidationDto);
        } else {
            //get the Brokerage Account transactions
            CompletableFuture<BrokerageAccountsTransactions> cfBrokerageAccountsTransactions =
                    brokerageTransactionService.getBrokerageAccountsTransactionsAsync(userAccounts, userRequest);
            //get the transactions for all TA accounts
            MutualFundAccountsTransactions mutualFundAccountsTransactions = mutualFundTransactionService.getMutualFundAccountsTransactions(
                    userAccounts, mergedExpandedAccountData.getMergedTAAccountIds(), userRequest);
            //Get Merged Expanded transactions from TA transactions
            MergedExpandedAccountTransactions mergedExpandedAccountTransactions = mergedExpandedTAAccountsService
                    .getMergedExpandedAccountsTransactions(mergedExpandedAccountData, mutualFundAccountsTransactions);

            AccountsTransactions accountsTransactions = divCapGainsBuilder.buildDistributionAccountsTransactions(
                    cfBrokerageAccountsTransactions, mutualFundAccountsTransactions,
                    mergedExpandedAccountTransactions, mergedExpandedAccountData.getMergedTAAccountIds());
            //Get Security Details from BFP Service
            List<SecurityDetail> securityDetails = securityRetrievalService.getSecurityDetails(accountsTransactions);

            distributions = distributionsResponseMapper.mapResults(accountsTransactions, userAccounts, userRequest, securityDetails, userAccountValidationDto);
        }
        return distributions;
    }
}

Explain the code line by line