<?php


namespace App\Classes\Modules\Transactions\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\ListsTransactions;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Vouchers\Services\FetchesVoucherRedemption;
use App\Classes\Modules\Vouchers\Services\CreatesVoucherRedemption;
use App\Classes\Modules\Vouchers\Services\RollbacksRedemption;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\BookingResource;
use App\Http\Resources\TransactionResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SuspendTransactionLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Suspend Transaction',
            'message' => 'You have successfully suspended the payment booking'
        ];
    }

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

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

    /** @var RollbacksRedemption */
    private $rollbacksRedemption;

    /** @var FetchesVoucherRedemption */
    private $fetchesVoucherRedemption;

    /** @var CreatesVoucherRedemption */
    private $createsVoucherRedemption;

    /**
     * SuspendTransactionLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, RollbacksRedemption $rollbacksRedemption, FetchesVoucherRedemption $fetchesVoucherRedemption, CreatesVoucherRedemption $createsVoucherRedemption)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->rollbacksRedemption = $rollbacksRedemption;
        $this->fetchesVoucherRedemption = $fetchesVoucherRedemption;
        $this->createsVoucherRedemption = $createsVoucherRedemption;
    }


    public function logic(Request $request) : JsonResponse
    {

        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('id')]);

        $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::SUSPENDED);

        if($transaction->voucherRedemption) {
            $result = $this->rollbacksRedemption->execute($transaction->voucherRedemption->redemption_id);
            if($result){
                $redemptionId = $result->id;
                $this->createsVoucherRedemption->execute($transaction, $transaction->voucherRedemption->voucher, $redemptionId, $transaction->voucherRedemption->value);
            }
        }

        return $this->response([]);

    }



}
