<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;


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

class CreateBookingRefundLogic extends AbstractControllerLogic
{

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

    /** @var FetchesBookingQuotation */
    private $fetchBookingQuotation;

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

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

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

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

    /** @var CalculatesBookingRefundAmount */
    private $calculatesBookingRefundAmount;

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

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

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

        $booking = $transaction->owner;

        $billNumber = $this->generatesTransactionBillNumber->execute('RFD-');

        $refund = $transaction->transactions()->refunds()->sum('amount');

        if($refund + $request->input('amount') > $transaction->original_amount) throw new MalformedRequestException('Your refund must not be greater than '. $transaction->original_amount .'.');

        $amount = $transaction->booking->fix_currency_id == 1 ? $request->input('amount') : $request->input('amount') / $transaction->currency_rate;


        $transactionRefundCalculationObject = new TransactionRefundCalculationObject($booking, $transaction, $amount);
        $transactionRefundCalculationObject->init();

        $object = new TransactionObject($billNumber, TransactionType::REFUND, 1, $booking->company->id,
            1, $transactionRefundCalculationObject->getConversionObject()->getPaymentMethod(),
            $transactionRefundCalculationObject->getRefundTotalAmount(), $transactionRefundCalculationObject->getAmount(), 1,
            $transactionRefundCalculationObject->getConversionObject()->getCurrencyId(), $transactionRefundCalculationObject->getTransaction()->currency_rate,
            $transactionRefundCalculationObject->getRefundTax(), $transactionRefundCalculationObject->getRefundServiceCharge(), null, ApprovalStatus::PENDING_VERIFICATION, [], $transaction->bill_no);

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

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


}