<?php

namespace App\Classes\Modules\Bookings\Standards\Criterias;


use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\General\Eloquent\AbstractFetchRecord;
use App\Classes\General\Eloquent\AbstractGetRecord;
use App\Classes\Modules\Bookings\DataTransferObjects\BookingMergeObject;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;

class BookingsAreMergeable
{

    /** @var Booking */
    private $repository;

    /**
     * BookingsAreMergeable constructor.
     * @param Booking $repository
     */
    public function __construct(Booking $repository)
    {
        $this->repository = $repository;
    }


    /**
     * @param BookingMergeObject $object
     * @return bool
     * @throws ResourceNotFoundException
     */
    public function execute(BookingMergeObject $object){

        $bookingOriginal=$this->repository->where(['id'=> $object->getId(),'status' => ApprovalStatus::APPROVED,])->first();
        $bookingToMerge=$this->repository->whereIn('id', $object->getBookingIds())
            ->where([
                'status' => ApprovalStatus::APPROVED,
                'service_id' => $bookingOriginal->service_id,
                'fix_currency_id' => $bookingOriginal->fix_currency_id,
                'convertible_currency_id' => $bookingOriginal->convertible_currency_id,
            ])->get();

        if($bookingOriginal===null || $bookingToMerge->count() !== count($object->getBookingIds())){
            throw new ResourceNotFoundException('The booking doesn\'t exist');
        }
        return true;

    }

}