<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;


use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionRefundCalculationObject;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Http\Resources\TransactionResource;
use App\Models\Booking;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateBookingRefundCreditNoteLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create Refund Booking',
            'message' => 'You have successfully created a refund booking'
        ];
    }

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

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

    /** @var GeneratesTransactionBillNumber */
    private $generatesTransactionBillNumber;

    /** @var CreatesTransaction */
    private $createsTransaction;

    /**
     * CreateBookingPaymentLogic constructor.
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     * @param CreatesTransaction $createsTransaction
     */
    public function __construct(FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction)
    {
        $this->fetchesTransaction = $fetchesTransaction;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
        $this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
        $this->createsTransaction = $createsTransaction;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {

        $booking = Booking::find($request->route('id'));

        $transaction = $this->fetchesTransaction->execute(['id' => $request->route('payment_id')]);
        
        $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::APPROVED);

        $paymentReferenceTransaction = $this->fetchesTransaction->execute(['bill_no' => $transaction->payment_reference]);

        if((int) $paymentReferenceTransaction->type === TransactionType::BILL){
            $transactionRefundCalculationObject = new TransactionRefundCalculationObject($booking, $transaction, $transaction->original_amount, $paymentReferenceTransaction->currency_rate);
            $transactionRefundCalculationObject->setRefundConversionAmount();

            $billNumber = $this->generatesTransactionBillNumber->execute('CRDNT-');
            
            $object = new TransactionObject($billNumber, TransactionType::CREDIT_NOTE, 1, $booking->company->id,
                $booking->bank_id, $paymentReferenceTransaction->payment_method,
                $transactionRefundCalculationObject->getRefundConversionAmount(), $transaction->original_amount, 1,
                $paymentReferenceTransaction->original_currency_id, $paymentReferenceTransaction->currency_rate,
                $paymentReferenceTransaction->tax, $paymentReferenceTransaction->service_charge, null, ApprovalStatus::PENDING_VERIFICATION, [], $paymentReferenceTransaction->bill_no);

            $transaction = $this->createsTransaction->execute($booking, $object);
        }

        return $this->resourceResponse(new TransactionResource($transaction));
    }


}