<?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\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionRefundCalculationObject;
use App\Classes\ValueObjects\Constants\PaymentMethodType;

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;

    /**
     * CreateBookingPaymentLogic constructor.
     * @param FetchesBookingQuotation $fetchBookingQuotation
     * @param FetchesTransaction $fetchesTransaction
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber
     * @param CreatesTransaction $createsTransaction
     */
    public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesTransaction $fetchesTransaction, UpdatesTransactionStatus $updatesTransactionStatus, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction)
    {
        $this->fetchBookingQuotation = $fetchBookingQuotation;
        $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
    {

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

        if ($transaction->transactions()->bills()->first()) {
            throw new MalformedRequestException('Booking under white form cannot request for refund');
        }

        $booking = $transaction->owner;

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

        $refund = $transaction->transactions()->refunds()->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED])->sum('original_amount');

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

        // $transactionRefundCalculationObject = new TransactionRefundCalculationObject($booking, $transaction, $request->input('amount'));
        // $transactionRefundCalculationObject->init();

        $refundAmount = bcdiv($request->input('amount'), $transaction->currency_rate, 7);
        // refund service charges if is fully refund
        $refundTotal = ($refund + $request->input('amount')) == $transaction->original_amount ? $refundAmount + $transaction->service_charge + $transaction->tax : $refundAmount;

        $object = new TransactionObject($billNumber, TransactionType::REFUND, 1, $booking->company->id,
            1, PaymentMethodType::CASH,
            $refundTotal, $request->input('amount'), 1,
            $transaction->original_currency_id, $transaction->currency_rate,
            0, 0, null, ApprovalStatus::PENDING_VERIFICATION, [], $transaction->bill_no);

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

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


}