<?php

namespace App\Http\Resources\Wallet;

use App\Repositories\Currency\CurrencyRepository;
use Illuminate\Http\Resources\Json\JsonResource;

class Wallet extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $usdBalanceAvailable = (new CurrencyRepository())->calculateAmountInUsd($this->currency, $this->balance_in_wallet, 2);
        $usdBalanceOrder = (new CurrencyRepository())->calculateAmountInUsd($this->currency, $this->balance_in_order, 2);
        $usdBalanceWithdraw = (new CurrencyRepository())->calculateAmountInUsd($this->currency, $this->balance_in_withdraw, 2);

        $inWallet = math_formatter($this->balance_in_wallet, $this->currency->decimals);
        $inWithdraw = math_formatter($this->balance_in_withdraw, $this->currency->decimals);
        $inOrder = math_formatter($this->balance_in_order, $this->currency->decimals);


        return [
            'currency' => $this->currency->name,
            'symbol' => $this->currency->symbol,
            'logo' => $this->currency->file ? url($this->currency->file->path) : null,
            'type' => $this->currency->type,
            'is_token' => $this->currency->is_token,
            'address' => $this->address ? $this->address->address : null,
            'payment_id' => $this->address ? $this->address->payment_id : null,

            'balance_in_wallet' => $inWallet > 0 ? $inWallet : 0,
            'balance_in_wallet_usd' => $usdBalanceAvailable > 0 ? $usdBalanceAvailable : 0,

            'balance_in_order' => $inOrder > 0 ? $inOrder : 0,
            'balance_in_order_usd' => $usdBalanceOrder > 0 ? $usdBalanceOrder : 0,

            'balance_in_withdraw' => $inWithdraw > 0 ? $inWithdraw : 0,
            'balance_in_withdraw_usd' =>  $usdBalanceWithdraw > 0 ? $usdBalanceWithdraw : 0,
        ];
    }
}
