<?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\CanUpdateBooking;
use App\Classes\Modules\Bookings\Services\UpdatesBookingOwner;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Bookings\Standards\Rules\CanFetchBooking;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Models\Company;

class UpdateBookingOwnerLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification(): array
    {
        return [
            'title' => 'Update Booking Owner',
            'message' => "You have successfully updated booking's owner"
        ];
    }

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

    /** @var UpdatesBookingOwner */
    private $updatesBookingOwner;

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

    /** @var CanFetchBooking */
    private $canFetchBooking;

    /** @var FetchesCompany */
    private $fetchesCompany;

    /**
     * UpdateBookingLogic constructor.
     * @param CanUpdateBooking $canUpdateBooking
     * @param UpdatesBookingOwner $updatesBookingOwner
     * @param CanFetchBooking $canFetchBooking
     * @param FetchesBooking $fetchesBooking
     * @param FetchesCompany $fetchesCompany
     */
    public function __construct(
        CanUpdateBooking $canUpdateBooking,
        UpdatesBookingOwner $updatesBookingOwner,
        CanFetchBooking $canFetchBooking,
        FetchesBooking $fetchesBooking,
        FetchesCompany $fetchesCompany
    ) {
        $this->canUpdateBooking = $canUpdateBooking;
        $this->updatesBookingOwner = $updatesBookingOwner;
        $this->canFetchBooking = $canFetchBooking;
        $this->fetchesBooking = $fetchesBooking;
        $this->fetchesCompany = $fetchesCompany;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request): JsonResponse
    {
        $this->canFetchBooking->passes();

        $booking = $this->fetchesBooking->execute([
            'id' => $request->route('id'),
        ]);

        $newCompanyId = Company::where('reference', $request->input('newMarking'))->first()->id;

        $this->updatesBookingOwner->execute($booking, $newCompanyId);

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