<?php

namespace App\Classes\Modules\Rules\Standards\Rules;


use App\Classes\Exceptions\CriteriaNotFulfilledException;
use App\Classes\General\Abstracts\AbstractRule;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\ValueObjects\Constants\KVPKey;
use App\Classes\ValueObjects\Constants\RoleTypes;
use Illuminate\Support\Facades\Auth;

class CanPassTransferPeriodLockRule extends AbstractRule
{

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


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

    /**
     * @return bool
     */
    protected function authorized($object): bool
    {
        return true;

    }

    /**
     * @return bool
     */
    protected function validators($object): bool
    {
        return true;

    }


    /**
     * @return bool
     */
    protected function criteria($object): bool
    {
        $booking = $this->fetchesBooking->execute(['id' => $object->bookingId]);
        $bookingAttribute = $booking->attributesKVP()->where('key', KVPKey::BOOKING_IS_LOCKED)->first();

        if($bookingAttribute && in_array(Auth::user()->type, RoleTypes::ADMIN_ROLES)){
            throw new CriteriaNotFulfilledException("Booking is locked for edit.");
        }
        return true;
    }
}
