<?php

namespace App\Services\Paddle;

use Illuminate\Support\Arr;

class PaddleSubscriptionService
{
    public function __construct(private readonly PaddleClient $client) {}

    /**
     * Retrieve a subscription record directly from Paddle.
     *
     * @return array<string, mixed>
     */
    public function retrieve(string $subscriptionId): array
    {
        $response = $this->client->get("/subscriptions/{$subscriptionId}");

        return is_array($response) ? $response : [];
    }

    /**
     * Convenience helper to extract metadata from the subscription response.
     *
     * @param  array<string, mixed>  $subscription
     * @return array<string, mixed>
     */
    public function metadata(array $subscription): array
    {
        $customData = Arr::get($subscription, 'data.custom_data');

        if (is_array($customData)) {
            return $customData;
        }

        $metadata = Arr::get($subscription, 'data.metadata');

        return is_array($metadata) ? $metadata : [];
    }
}
