<?php

namespace App\Http\Helpers;

use Illuminate\Support\Facades\Http;

class CurrencyConversion
{

    public static $currencies = 'USD,EGP,SAR,AED';

    public static function getCurrentRatesInUSD()
    {

        $response = Http::get(
            env('EXCHANGE_RATES_API_URL')
            . '?access_key=' .
            env('EXCHANGE_RATES_API_KEY') . '&symbols=' . CurrencyConversion::$currencies
        );
        //dd($response->json());
        // loop over the results
        $rates = $response->json()['rates']??[
            'USD'=>1,
            'SAR'=>3.77,
            'AED'=>3.75,
            'EGP'=>29.55,
        ];

        // loop over the rates
        foreach ($rates as $key => $value) {
            if($key != 'USD')
                $rates[$key] = $value / $rates['USD'];
        }
        $rates['USD'] = 1;

        return $rates;
    }
}
