<?php

namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Transactions\Services\DeletesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Transactions\Processors\UpdateRefundTransactionStatusProcessor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Http\Resources\GroupResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class DeleteGroupLogic extends AbstractControllerLogic
{

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

    /** @var UpdatesTransactionStatus */
    private $updatesTransactionStatus;

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

    /** @var DeletesTransaction */
    private $deletesTransaction;

    /** @var UpdateRefundTransactionStatusProcessor */
    private $updateRefundTransactionStatusProcessor;

    /**
     * DeleteGroupLogic constructor.
     * @param updatesTransactionStatus $updatesTransactionStatus
     * @param FetchesGroup $fetchesGroup
     * @param DeletesTransaction $deletesTransaction
     * @param UpdateRefundTransactionStatusProcessor $updateRefundTransactionStatusProcessor
     */
    public function __construct(updatesTransactionStatus $updatesTransactionStatus, FetchesGroup $fetchesGroup, DeletesTransaction $deletesTransaction, UpdateRefundTransactionStatusProcessor $updateRefundTransactionStatusProcessor)
    {
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->fetchesGroup = $fetchesGroup;
        $this->deletesTransaction = $deletesTransaction;
        $this->updateRefundTransactionStatusProcessor = $updateRefundTransactionStatusProcessor;
    }

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

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

        foreach($items as $item) {
            $bill = $item;
            $payment = $bill->owner;
            $group->transactions()->detach($bill->id);

            $trns = $payment->transactions()->where('type', TransactionType::BILL)->with('groupTransaction')->get();
            if(count($trns) <= 1){
                $this->updatesTransactionStatus->execute($payment, ApprovalStatus::APPROVED);
            }
            $this->deletesTransaction->execute($bill);
            $this->checkForRefund($payment);
        }

        $group->delete();

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

    //Check for refund if it is still pending, auto reject immediately
    private function checkForRefund($payment){
        $refunds = $payment->transactions()->refunds()->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION])->get();
        foreach($refunds as $refund) {
            $this->updateRefundTransactionStatusProcessor->execute($refund->id, ApprovalStatus::REJECTED);
        }
    }
}
