<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Bookings\Standards\Rules\CanCreateBooking;
use App\Classes\Modules\Bookings\Services\FetchesBooking;
use App\Classes\Modules\KeyValuePairs\Services\CreatesKeyValuePair;
use App\Classes\ValueObjects\Constants\KVPKey;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateBookingLockLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Booking Locked',
            'message' => 'You have successfully lock Booking from editing'
        ];
    }

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

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


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

    /** @var CreatesKeyValuePair */
    private $createsKeyValuePair;

    /**
     * CreateBookingLockLogic constructor.
     * @param CanCreateBooking $canCreateBooking
     * @param FetchesCompany $fetchesCompany
     * @param CreatesKeyValuePair $createsKeyValuePair
     * @param FetchesBooking $fetchesBooking
     */
    public function __construct(CanCreateBooking $canCreateBooking, FetchesCompany $fetchesCompany, CreatesKeyValuePair $createsKeyValuePair, FetchesBooking $fetchesBooking)
    {
        $this->canCreateBooking = $canCreateBooking;
        $this->fetchesCompany = $fetchesCompany;
        $this->createsKeyValuePair = $createsKeyValuePair;
        $this->fetchesBooking = $fetchesBooking;
    }


    /**
     * @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
    {
        // cief todo: 110 - CanCreateBookingLock

        // $password = $request->input('password');
        // if($password !== 'prototype'){
        //     throw new AccessForbiddenException('Incorrect Password');
        // }

        $booking = $this->fetchesBooking->execute(['id' => $request->input('booking_id')]);
        $this->updateOrCreateKeyValuePair($booking, KVPKey::BOOKING_IS_LOCKED, 1);

        return $this->response([]);
    }

    private function updateOrCreateKeyValuePair($booking, $key, $value)
    {
        $booking->attributesKVP()->updateOrCreate(
            ['key' => $key],
            ['value' => $value]
        );
    }
}
