<?php

namespace Gaco\SnapBi\Services\Response;

use Illuminate\Http\JsonResponse;

/**
 * Token Response Handler
 * 
 * Handles B2B Access Token response with specific structure:
 * - accessToken
 * - tokenType
 * - expiresIn
 */
class TokenResponse extends AbstractResponse
{
    /**
     * Return success response for B2B Access Token
     * 
     * @param array $data Token data with keys: accessToken, tokenType, expiresIn
     * @return JsonResponse
     */
    public function success(array $data = []): JsonResponse
    {
        // Validate that httpCode is not provided (success always uses 200)
        if (isset($data['httpCode'])) {
            throw new \InvalidArgumentException(
                'httpCode parameter is not allowed in success() method. Success response always uses HTTP 200.'
            );
        }

        // Filter out error-related parameters
        $filteredData = $this->filterSuccessData($data);

        // Get additionalInfo using parent method (respects null/empty check)
        $additionalInfo = $this->getAdditionalInfo($filteredData);

        // Ensure required token fields are present
        $tokenData = [
            'accessToken' => $filteredData['accessToken'] ?? '',
            'tokenType' => $filteredData['tokenType'] ?? 'Bearer',
            'expiresIn' => $filteredData['expiresIn'] ?? '900',
        ];

        return $this->builder->success(
            serviceCode: $this->serviceCode->value,
            data: $tokenData,
            additionalInfo: $additionalInfo ?? [],
            message: 'Successful'
        );
    }
}
