<?php

namespace App\Classes\Modules\Wallets\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Wallets\DataTransferObjects\WalletObject;
use App\Classes\Modules\Wallets\Services\CreatesWallet;
use App\Classes\Modules\Wallets\Services\GeneratesWalletCode;
use App\Classes\Modules\Wallets\Standards\Rules\CanCreateCompanyWallet;
use App\Http\Resources\WalletResource;
use App\Classes\Modules\Wallets\Services\FetchesWallet;
use App\Models\Wallet;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;

class FetchWalletByCompanyModuleControllerLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Retrieved Wallet',
            'message' => 'You have successfully retrieved a wallet'
        ];
    }

    /** @var FetchesWallet  */
   private $fetchesWallet;

    /** @var CreatesWallet  */
    private $createsWallet;

    /** @var GeneratesWalletCode */
    private $generatesWalletCode;

    /** @var CanCreateCompanyWallet */
    private $canCreateCompanyWallet;

    /** @var FetchesCompanyModule */
    private $fetchesCompanyModule;
    

    /**
     * CreateWalletLogic constructor.
     * @param FetchesWallet $fetchesWallet
     */
    public function __construct(FetchesWallet $fetchesWallet, CreatesWallet $createsWallet, GeneratesWalletCode $generatesWalletCode, CanCreateCompanyWallet $canCreateCompanyWallet, FetchesCompanyModule $fetchesCompanyModule)
    {
        $this->fetchesWallet = $fetchesWallet;
        $this->fetchesCompanyModule = $fetchesCompanyModule;
        $this->createsWallet = $createsWallet;
        $this->generatesWalletCode = $generatesWalletCode;
        $this->canCreateCompanyWallet = $canCreateCompanyWallet;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        // $this->canFetchWallet->passes();

        if (!count(Wallet::where('owner_id', $request->route('company_module_id'))->get())) {
            $object = new WalletObject($request->route('company_module_id'), $request->input('currency_id') ?? 1, $this->generatesWalletCode->execute());

            $this->canCreateCompanyWallet->passes($object);
    
            $company_module = $this->fetchesCompanyModule->execute(['id' => $request->route('company_module_id')]);
    
            $wallet = $this->createsWallet->execute($object, $company_module);
    
            return $this->resourceResponse(new WalletResource($wallet));
        }

        $filters = $this->fetchesWallet->deserializeFilters($request->input('filters'));

        $filters['owner_id'] = $request->route('company_module_id');

        $query = $this->fetchesWallet->execute($filters);

        return $this->resourceResponse(new WalletResource($query));
    }
}
