<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\Modules\Bookings\Services\UpdatesBookingFixedAmount;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Http\Resources\BookingResource;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Bookings\Services\FetchesBooking;

use App\Classes\Modules\Bookings\Standards\Rules\CanUpdateBooking;
use App\Classes\Modules\Bookings\DataTransferObjects\BookingObject;
use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Classes\Exceptions\MalformedRequestException;

class UpdateBookingAmountLogic extends AbstractControllerLogic
{


    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Booking',
            'message' => 'You have successfully updated the Booking'
        ];
    }

    /** @var CanUpdateBooking */
    private $canUpdateBooking;

    /** @var UpdatesBookingFixedAmount */
    private $updatesBookingFixedAmount;

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

    /** @var CalculatesBookingOutstanding */
    private $calculatesBookingOutstanding;

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

    /**
     * UpdateBookingAmountLogic constructor.
     * @param CanUpdateBooking $canUpdateBooking
     * @param UpdatesBookingFixedAmount $updatesBookingFixedAmount
     * @param FetchesBooking $fetchesBooking
     * @param CalculatesBookingOutstanding $calculatesBookingOutstanding
     * @param UpdatesTransactionStatus $updatesTransactionStatus
     */
    public function __construct(CanUpdateBooking $canUpdateBooking, UpdatesBookingFixedAmount $updatesBookingFixedAmount, FetchesBooking $fetchesBooking, CalculatesBookingOutstanding $calculatesBookingOutstanding, UpdatesTransactionStatus $updatesTransactionStatus)
    {
        $this->canUpdateBooking = $canUpdateBooking;
        $this->updatesBookingFixedAmount = $updatesBookingFixedAmount;
        $this->fetchesBooking = $fetchesBooking;
        $this->calculatesBookingOutstanding = $calculatesBookingOutstanding;
        $this->updatesTransactionStatus = $updatesTransactionStatus;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]);

        $input_amount = number_format( floatval(str_replace(',', '', $request->input('fix_amount', $booking->fix_amount))), 5, '.', '');

        $minimum_amount = $booking->fix_amount - $this->calculatesBookingOutstanding->execute($booking);
        
        if ((float)$input_amount < $minimum_amount) {
            throw new MalformedRequestException('Booking Amount cannot be less than '. $minimum_amount .'.');
        }

        $poTransaction = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first();
        $booking->transactions()->where('type', TransactionType::PROFORMA)->delete();

        if($poTransaction) {
            $this->updatesTransactionStatus->execute($poTransaction, ApprovalStatus::PENDING_SUBMISSION);
        }

        $booking = $this->updatesBookingFixedAmount->execute($booking, $input_amount);

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

}