<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Bookings\Standards\Rules\CanFetchBooking;
use App\Classes\Modules\Transactions\Processors\CreateReceiptVoucherTransactionProcessor;
use App\Http\Resources\BookingResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class RegenerateBookingPaymentRVLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Regenerate Booking Payment Receipt Voucher',
            'message' => 'You have successfully regenerate booking payment receipt voucher'
        ];
    }

    /** @var CanFetchBooking */
    private $canFetchBooking;

    /** @var FetchesBooking */
    private $fetchesBooking;

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

    /** @var CreateReceiptVoucherTransactionProcessor */
    private $createReceiptVoucherTransactionProcessor;

    /**
     * RegenerateBookingPaymentRVLogic constructor.
     * @param CanFetchBooking $canFetchBooking
     * @param FetchesBooking $fetchesBooking
     * @param FetchesTransaction $fetchesTransaction
     * @param CreateReceiptVoucherTransactionProcessor $createReceiptVoucherTransactionProcessor
     */
    public function __construct(
        CanFetchBooking $canFetchBooking,
        FetchesBooking $fetchesBooking,
        FetchesTransaction $fetchesTransaction,
        CreateReceiptVoucherTransactionProcessor $createReceiptVoucherTransactionProcessor
    ) {
        $this->canFetchBooking = $canFetchBooking;
        $this->fetchesBooking = $fetchesBooking;
        $this->fetchesTransaction = $fetchesTransaction;
        $this->createReceiptVoucherTransactionProcessor = $createReceiptVoucherTransactionProcessor;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request): JsonResponse
    {
        $this->canFetchBooking->passes();

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

        if($booking && $transaction){
            $this->createReceiptVoucherTransactionProcessor->execute($booking, $transaction, true);
        }

        return $this->resourceResponse(new BookingResource($booking));
    }
}
