<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\DataTransferObjects\WaiveTransactionObject;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionIsWaived;
use App\Classes\Modules\Transactions\Standards\Rules\CanWaiveTransaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class WaiveTransactionLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Waive Transaction',
            'message' => 'You have successfully waived the transaction'
        ];
    }

    /** @var FetchesTransaction */
    private $fetchesTransaction;

    /** @var UpdatesTransactionIsWaived */
    private $updatesTransactionIsWaived;

    /** @var CanWaiveTransaction */
    private $canWaiveTransaction;


    /**
     * WaiveTransactionLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionIsWaived $updatesTransactionIsWaived
     * @param CanWaiveTransaction $canWaiveTransaction
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionIsWaived $updatesTransactionIsWaived, CanWaiveTransaction $canWaiveTransaction)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionIsWaived = $updatesTransactionIsWaived;
        $this->canWaiveTransaction = $canWaiveTransaction;
    }


    public function logic(Request $request) : JsonResponse
    {
        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $object = new WaiveTransactionObject($transaction->id, $transaction->bill_no, $transaction->type, $transaction->status);
        $this->canWaiveTransaction->passes($object);

        $this->updatesTransactionIsWaived->execute($transaction);
        Log::info(Auth::user()->email." waive storage invoice charges with bill_no: ".$transaction->bill_no);

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