<?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\DeletesGroup;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DeleteGroupLogic extends AbstractControllerLogic
{

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

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

    /** @var DeletesGroup */
    private $deletesGroup;

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

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        ini_set('memory_limit', '-1'); 

        $group = $this->fetchesGroup->execute(['id' => $request->route('id')]);

        foreach ($group->groupTransactions as $groupTransaction) {
            // $groupTransaction->transaction->delete();
            $groupTransaction->delete();
        }

        $this->deletesGroup->execute($group);

        return $this->response([]);
    }

}