<?php

namespace Themes\Pro\modunawa\app\Http\Controllers\Shop;

use App\Http\Controllers\Controller;
use App\Services\CallApiService;
use Themes\Pro\modunawa\app\Services\MsqMarketApiService;

class WalletAddController extends Controller
{
    private $callApiService;
    private $msqMarketApiService;

    public function __construct(CallApiService $callApiService, MsqMarketApiService $msqMarketApiService)
    {
        $this->callApiService = $callApiService;
        $this->msqMarketApiService = $msqMarketApiService;
    }

    public function store()
    {
        $prepareConnection = $this->msqMarketApiService->prepareConnection();

        if ($this->callApiService->verifyApiError($prepareConnection)) {
            notify()->error($prepareConnection['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        $establishConnection = $this->msqMarketApiService->establishConnection($prepareConnection['uuid'], request('token'));

        if ($this->callApiService->verifyApiError($establishConnection)) {
            notify()->error($establishConnection['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        $getBalance = $this->msqMarketApiService->getBalance($establishConnection['token']);

        if ($this->callApiService->verifyApiError($getBalance)) {
            notify()->error($getBalance['body'], 'Error', 'bottomRight');
            return redirect()->back();
        }

        session()->put('wallet.main', array_merge($getBalance['userInfo'], [ 'token' => $establishConnection['token'] ]));

        notify()->success(_e('Action completed'), 'Success', 'bottomRight');
        return redirect()->back();
    }
}
