<?php
namespace App\Services\Currency;

use App\Models\Country;
use App\Services\Auth\User\AccountService;

class CurrencyService
{
    public function getCurrency()
    {
        $user = (new AccountService())->getAccount();

        if($user['user'])
        {
            $currency = Country::withDescription($user['user']->currency_id)->first();
        }
        else
        {
            $currency_id = session('currency_id');
            $currency = Country::withDescription($currency_id)->first();
        }
        if(is_null($currency))
        {
            $currency = Country::withDescription(1)->first();
        }

        return $currency;
    }
    public function setCurrency($country)
    {
        $user = (new AccountService())->getAccount();

        if($user['user'])
        {
            $user['user']->update(['currency_id'=>$country->id]);
        }

        return $this->getCurrency();
    }
    public function getCurrentCountryFromIp($ip=null) {
       $response = \Http::get('http://ip-api.com/json/'.$ip);
       return $response->json();
    }

}
