<?php

namespace App\Http\Requests\Api\Wallet\Rules;

use App\Models\Network\Network;
use App\Repositories\Currency\CurrencyRepository;
use App\Repositories\Network\NetworkRepository;
use Illuminate\Contracts\Validation\Rule;

class WalletWithdrawNetworkRule implements Rule
{
    /**
     * @var NetworkRepository
     */
    private $networkRepository, $error;

    public function __construct()
    {
        $this->networkRepository = new NetworkRepository();
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string $attribute
     * @param  string $uuid
     * @return bool
     */
    public function passes($attribute, $networkId)
    {
        if(!$this->networkRepository->getById($networkId)) {
            $this->error = 'Invalid Network';
            return false;
        }

        $currency = (new CurrencyRepository())->getCurrencyBySymbol(request()->get('symbol'));
        $network = Network::where('id', $networkId)->first();



        if(!$network->withdraw_status || ($currency && in_array($networkId, $currency->disabled_withdrawal_networks))) {
            $this->error = 'Withdrawals are not allowed for this network';
            return false;
        }

        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return __($this->error);
    }
}
