<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ExpireBookingPaymentControllerLogic extends AbstractControllerLogic
{

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

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

    /**
     * DeletePurchaseOrderPdfLogic constructor.
     * @param FetchesBooking $fetchesBooking
     */
    public function __construct(fetchesBooking $fetchesBooking)
    {
        $this->fetchesBooking = $fetchesBooking;
    }


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

        $payment = $booking->transactions()
            ->payments()->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
            ->first();

        $payment->status = ApprovalStatus::EXPIRED;
        $payment->save();

        return $this->response([]);
    }

}
