<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Banks\Services\FetchesBank;
use App\Classes\Modules\Currencies\Services\FetchesCurrency;

use App\Classes\Modules\Bookings\Standards\Rules\CanCreateBooking;
use App\Classes\Modules\Bookings\Services\CreatesBooking;
use App\Classes\Modules\Bookings\Services\GeneratesBookingMarking;

use App\Classes\Modules\Bookings\DataTransferObjects\BookingObject;
use App\Http\Resources\BookingResource;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateBookingLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Booking',
            'message' => 'You have successfully created a new Booking'
        ];
    }

    /** @var CanCreateBooking */
    private $canCreateBooking;

    /** @var CreatesBooking */
    private $createsBooking;

    /** @var GeneratesBookingMarking */
    private $generatesBookingMarking;

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

    /**
     * CreateBookingLogic constructor.
     * @param CanCreateBooking $canCreateBooking
     * @param CreatesBooking $createsBooking
     * @param GeneratesBookingMarking $generatesBookingMarking
     * @param FetchesCompany $fetchesCompany
     */
    public function __construct(CanCreateBooking $canCreateBooking, CreatesBooking $createsBooking, GeneratesBookingMarking $generatesBookingMarking, FetchesCompany $fetchesCompany)
    {
        $this->canCreateBooking = $canCreateBooking;
        $this->createsBooking = $createsBooking;
        $this->generatesBookingMarking = $generatesBookingMarking;
        $this->fetchesCompany = $fetchesCompany;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $company = $this->fetchesCompany->execute(['id' => $request->input('company_id')]);

        $object = new BookingObject($request->input('service_id'), $request->input('transferable_bank_id'), $this->generatesBookingMarking->execute(), number_format( floatval(str_replace(',', '', $request->input('fix_amount'))), 5, '.', ''), $request->input('type') === 1 ? $request->input('convertible_currency_id') : 1, $request->input('convertible_currency_id'), 1);

        $this->canCreateBooking->passes($object);

        $booking = $this->createsBooking->execute($company, $object);

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

    }
}