<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Wallets\Standards\Rules\CanCreateWalletTransaction;
use App\Classes\Modules\Wallets\DataTransferObjects\WalletTransactionObject;
use App\Classes\Modules\Wallets\Services\CreatesWalletTransaction;
use App\Classes\Modules\Wallets\Services\GeneratesWalletTransactionBillNo;
use App\Http\Resources\WalletTransactionResource;
use App\Classes\Modules\Wallets\Services\FetchesWallet;

use App\Classes\Modules\Currencies\Services\FetchesCurrency;
use App\Classes\Modules\Currencies\Services\RateCalculatesCurrency;
/*
use App\Classes\Modules\Accounts\Standards\Rules\CanCreateUser;
use App\Classes\Modules\Accounts\Services\CreatesUser;
use App\Classes\Modules\Accounts\DataTransferObjects\UserObject;
use App\Http\Resources\UserResource;

use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompany;
use App\Classes\Modules\Companies\Services\CreatesCompany;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyObject;

use App\Classes\Modules\Contacts\Standards\Rules\CanCreateContact;
use App\Classes\Modules\Contacts\Services\CreatesContact;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;

use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompanyEmployee;
use App\Classes\Modules\Companies\Services\CreatesCompanyEmployee;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyEmployeeObject;

use App\Classes\Modules\SegmentCompanies\Standards\Rules\CanCreateSegmentCompany;
use App\Classes\Modules\SegmentCompanies\Services\CreatesSegmentCompany;
use App\Classes\Modules\SegmentCompanies\DataTransferObjects\SegmentCompanyObject;
*/
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CreateWalletTransactionLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Wallet Transaction',
            'message' => 'You have successfully created a wallet transaction'
        ];
    }

    /** @var CreatesWalletTransaction  */
   private $createsWalletTransaction;

   /** @var GeneratesWalletTransactionBillNo */
   private $generatesWalletTransactionBillNo;

   /** @var CanCreateWalletTransaction */
   private $canCreateWalletTransaction;
   
   private $fetchesCurrency;
   
   private $rateCalculatesCurrency;
   
   private $fetchesWallet;

    /**
     * CreateWalletLogic constructor.
     * @param CreatesWalletTransaction $createsWalletTransaction
     * @param GeneratesWalletTransactionBillNo $generatesWalletTransactionBillNo
     * @param CanCreateWalletTransaction $canCreateWalletTransaction
     * @param FetchesCurrency $fetchesCurrency
     * @param RateCalculatesCurrency $rateCalculatesCurrency
     * @param FetchesWallet $fetchesWallet
     */
    public function __construct(
            CreatesWalletTransaction $createsWalletTransaction, GeneratesWalletTransactionBillNo $generatesWalletTransactionBillNo, CanCreateWalletTransaction $canCreateWalletTransaction,
            FetchesCurrency $fetchesCurrency,RateCalculatesCurrency $rateCalculatesCurrency, FetchesWallet $fetchesWallet
        )
    {
        $this->createsWalletTransaction = $createsWalletTransaction;
        $this->generatesWalletTransactionBillNo = $generatesWalletTransactionBillNo;
        $this->canCreateWalletTransaction = $canCreateWalletTransaction;
        $this->fetchesCurrency = $fetchesCurrency;
        $this->rateCalculatesCurrency = $rateCalculatesCurrency;
        $this->fetchesWallet = $fetchesWallet;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {

            DB::beginTransaction();
            
            $wallet =  $this->fetchesWallet->execute(['id' => $request->route('id')]);
            $conversion_currency = $this->fetchesCurrency->execute(['id' => $request->input('currency_id')]);
            $convertable_currency = $this->fetchesCurrency->execute(['id' => $wallet->currency_id]);//MYR
            
            $convert_amount = $this->rateCalculatesCurrency->execute($conversion_currency, $convertable_currency, $request->input('amount'));
            $convert_rate = $this->rateCalculatesCurrency->execute_rate($conversion_currency, $convertable_currency);
            

            $object = new WalletTransactionObject(
                $wallet->id, $this->generatesWalletTransactionBillNo->execute(),$request->input('trans_type'),
                number_format( (float) $convert_amount, 5, '.', ''), $wallet->currency_id , number_format( (float) $request->input('amount'), 5, '.', ''),
                $request->input('currency_id'),number_format( (float) $convert_rate, 5, '.', '')
            );

            $this->canCreateWalletTransaction->passes($object);

            $wallet_transaction = $this->createsWalletTransaction->execute($object);

            DB::commit();

            return $this->resourceResponse(new WalletTransactionResource($wallet_transaction));

        } catch (\Exception $exception) {
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }

    }
}