<?php

namespace Gaco\SnapBi\Services\Response;

/**
 * Virtual Account Response Handler
 * 
 * Unified response handler for all Virtual Account related services:
 * - VA Inquiry
 * - VA Payment
 * - Update Status VA
 * - Inquiry Status
 * - Transfer Inquiry/Commit
 * - Notify Payment Intrabank
 * 
 * All these services use the same structure:
 * - virtualAccountData
 * - additionalInfo
 */
class VirtualAccountResponse extends AbstractResponse
{
    /**
     * Format success data for Virtual Account services
     * 
     * Ensures virtualAccountData is properly wrapped
     */
    protected function formatSuccessData(array $data): array
    {
        // Extract additionalInfo before formatting
        $additionalInfo = $data['additionalInfo'] ?? null;
        
        // Remove additionalInfo from data to avoid duplication
        unset($data['additionalInfo']);
        
        // If virtualAccountData is not in data, wrap it
        if (!isset($data['virtualAccountData']) && !empty($data)) {
            return ['virtualAccountData' => $data];
        }

        return $data;
    }

    /**
     * Get additional info from data
     * 
     * Preserves additionalInfo for Virtual Account response
     */
    protected function getAdditionalInfo(array $data): ?array
    {
        // Check if additionalInfo is in the original data
        if (isset($data['additionalInfo'])) {
            return $data['additionalInfo'];
        }
        
        return null;
    }
}

