<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\Modules\Bookings\Services\UpdatesBookingFixedAmount;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\Bookings\Standards\Rules\CanUpdateBooking;
use App\Classes\Modules\KeyValuePairs\Services\CreatesKeyValuePair;
use App\Classes\Modules\KeyValuePairs\Services\UpdatesKeyValuePair;
use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\KeyValuePairs\DataTransferObjects\KeyValuePairObject;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Http\Resources\BookingResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use ErrorException;

class UpdateBookingAmountOnHoldLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Recorded Booking Amount',
            'message' => 'You have successfully recorded the Booking Amount to be updated'
        ];
    }

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

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

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

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

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

    /** @var CreatesKeyValuePair */
    private $createsKeyValuePair;

    /** @var UpdatesKeyValuePair */
    private $updatesKeyValuePair;

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

    /**
     * @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('amount_to_edit', $booking->fix_amount))), 5, '.', '');

        $minimum_amount = $booking->fix_amount - $this->calculatesBookingOutstanding->execute($booking);

        if (((float)$input_amount + 0.01) < (float)$minimum_amount) {
            throw new MalformedRequestException('Booking Amount cannot be less than '. $minimum_amount .'.');
        }

        $keyValuePairObject = new KeyValuePairObject(KVPKey::BOOKING_AMOUNT_UPDATE, $input_amount);
        $metadata = $booking->attributesKVP()->where('key', KVPKey::BOOKING_AMOUNT_UPDATE)->first();
        if($metadata){
            $this->updatesKeyValuePair->execute($metadata, $keyValuePairObject);
        }
        else{
            $this->createsKeyValuePair->execute($booking, $keyValuePairObject);
        }

        $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));
    }

}
