<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

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\CanDeleteBooking;
use App\Classes\Modules\Bookings\Services\DeletesBooking;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DeleteBookingLogic extends AbstractControllerLogic
{

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

    /** @var CanDeleteBooking */
    private $canDeleteBooking;

    /** @var DeletesBooking */
    private $deletesBooking;

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


    public function __construct(
        CanDeleteBooking $canDeleteBooking, 
        DeletesBooking $deletesBooking, 
        FetchesBooking $fetchesBooking
    )
    {
        $this->canDeleteBooking = $canDeleteBooking;
        $this->deletesBooking = $deletesBooking;
        $this->fetchesBooking = $fetchesBooking;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {
            DB::beginTransaction();

            $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]);
            $this->canDeleteBooking->passes();
            $this->deletesBooking->execute($booking);
            
            DB::commit();

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

        } catch (\Exception $exception){
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }
    }

}