<?php

namespace App\Services;

use GuzzleHttp\Client;

class CoinPriceService
{
    protected $client;

    public function __construct()
    {
        $this->client = new Client(['base_uri' => 'https://api.coingecko.com/api/v3/']);
    }

    public function getCoinPrice($coinId)
    {
        try {
            $response = $this->client->request('GET', 'simple/price', [
                'query' => [
                    'ids' => $coinId,
                    'vs_currencies' => 'usd'
                ]
            ]);

            $data = json_decode($response->getBody(), true);
            return $data[$coinId]['usd'] ?? null;
        } catch (\Exception $e) {
            \Log::error('Error fetching coin price: ' . $e->getMessage());
            return null;
        }
    }
}
