<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Http\Resources\GroupResource;
use App\Models\SegmentConstant;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\CalculatesTransactionServiceCharge;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Transactions\Services\UpdatesTransaction;
use App\Classes\Modules\Transactions\Services\CalculatesTransactionTransferFee;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use ErrorException;

class UpdateGroupLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Update Group Transaction',
            'message' => 'You have successfully updated this Group Transaction'
        ];
    }

    /** @var FetchesGroup */
    private $fetchesGroup;

    /** @var FetchesCompany */
    private $fetchesCompany;

    /** @var CalculatesTransactionServiceCharge */
    private $calculatesTransactionServiceCharge;

    /** @var UpdatesTransaction */
    private $updatesTransaction;

    /** @var CalculatesTransactionTransferFee */
    private $calculatesTransactionTransferFee;

    public function __construct(
        FetchesGroup $fetchesGroup,
        FetchesCompany $fetchesCompany,
        CalculatesTransactionServiceCharge $calculatesTransactionServiceCharge,
        UpdatesTransaction $updatesTransaction,
        CalculatesTransactionTransferFee $calculatesTransactionTransferFee
    )
    {
        $this->fetchesGroup = $fetchesGroup;
        $this->fetchesCompany = $fetchesCompany;
        $this->calculatesTransactionServiceCharge = $calculatesTransactionServiceCharge;
        $this->updatesTransaction = $updatesTransaction;
        $this->calculatesTransactionTransferFee = $calculatesTransactionTransferFee;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $group = $this->fetchesGroup->execute(['id' => $request->route('id')]);

        $transactions = $group->transactions()->get();

        $rate = $request->input('rate');

        $supplier = $this->fetchesCompany->execute(['id' => $request->input('supplier_id')]);

        foreach($transactions as $transaction) {

            $constant = SegmentConstant::where('reference', SegmentConstants::SERVICE_CHARGE)->where('detail->id', $supplier->id)->first();
            $serviceCharge = $this->calculatesTransactionServiceCharge->execute($transaction->original_amount, $rate, $constant);

            $object = new TransactionObject(
                $transaction->bill_no, 
                TransactionType::BILL, 
                $supplier->id, 
                1,
                $supplier->banks()->where('default', true)->first()->id, 
                PaymentMethodType::CASH,
                $transaction->original_amount * (1 / $rate), 
                $transaction->original_amount, 
                1, 
                $transaction->original_currency_id,
                $rate, 
                0, 
                $serviceCharge, 
                null, 
                ApprovalStatus::PENDING_VERIFICATION
            );

            $billTransaction = $this->updatesTransaction->execute($transaction, $object);

            $transferTransaction = $transaction->transactions()->where('type', TransactionType::TRANSFER_FEE)->first();

            $transferFee = $this->calculatesTransactionTransferFee->execute($billTransaction->amount, $constant);

            $object = new TransactionObject(
                $transferTransaction->bill_no, 
                TransactionType::TRANSFER_FEE, 
                1, 
                $supplier->id,
                $supplier->banks()->where('default', true)->first()->id, 
                PaymentMethodType::CASH,
                $transaction->original_amount, 
                $transaction->original_amount, 
                $transaction->original_currency_id, 
                $transaction->original_currency_id,
                1, 
                0, 
                $transferFee, 
                null, 
                ApprovalStatus::PENDING_VERIFICATION
            );

            $this->updatesTransaction->execute($transferTransaction, $object);
        }

        $group->issuer = $supplier;
        $group->amount = $group->transactions()->sum('amount');
        $group->currency_rate = $rate;
        $group->tax = $group->transactions()->sum('tax');
        $group->service_charge = $group->transactions()->sum('service_charge');

        $group->save();
        
        return $this->resourceResponse(new GroupResource($group));
    }

}