<?php

/**
 * This code was generated by
 * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
 *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
 *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
 *
 * Twilio Knowledge API
 * APIs for managing knowledge bases and knowledge content for AI-powered applications.
 *
 * NOTE: This class is auto generated by OpenAPI Generator.
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


namespace Twilio\Rest\Knowledge\V2;

use Twilio\Exceptions\TwilioException;
use Twilio\Options;
use Twilio\Values;
use Twilio\Version;
use Twilio\ApiV1Version;
use Twilio\InstanceContext;
use Twilio\Http\Response;
use Twilio\Metadata\ResourceMetadata;


class KnowledgeBasisContext extends InstanceContext
    {
    /**
     * Initialize the KnowledgeBasisContext
     *
     * @param Version $version Version that contains the resource
     * @param string $kbId A unique Knowledge Base ID using Twilio Type ID (TTID) format
     */
    public function __construct(
        Version $version,
        $kbId
    ) {
        $apiV1Version = new ApiV1Version($version->getDomain(), $version->version);
        parent::__construct($apiV1Version);

        // Path Solution
        $this->solution = [
        'kbId' =>
            $kbId,
        ];

        $this->uri = '/ControlPlane/KnowledgeBases/' . \rawurlencode($kbId)
        .'';
    }

    /**
     * Helper function for Delete
     *
     
     * @return Response Deleted Response
     * @throws TwilioException When an HTTP error occurs.
     */
    private function _delete(): Response
    {
        
        $headers = Values::of(['Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json' ]);
        return $this->version->handleRequest('DELETE', $this->uri, [], [], $headers, "delete");
    }

    /**
     * Delete the DeleteKnowledgeBase202ResponseInstance
     *
     
     * @return DeleteKnowledgeBase202ResponseInstance Deleted DeleteKnowledgeBase202ResponseInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function delete(): DeleteKnowledgeBase202ResponseInstance
    {
        $response = $this->_delete();
        
        return new DeleteKnowledgeBase202ResponseInstance(
            $this->version,
            $response->getContent(),
            $this->solution['kbId']
        );
    }

    /**
     * Delete the DeleteKnowledgeBase202ResponseInstance with Metadata
     *
     
     * @return ResourceMetadata The Deleted Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function deleteWithMetadata(): ResourceMetadata
    {
        $response = $this->_delete();
        
        $resource = new DeleteKnowledgeBase202ResponseInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['kbId']
                    );
        return new ResourceMetadata(
            $resource,
            $response->getStatusCode(),
            $response->getHeaders()
        );
    }


    /**
     * Helper function for Fetch
     *
     
     * @return Response Fetched Response
     * @throws TwilioException When an HTTP error occurs.
     */
    private function _fetch(): Response
    {
        
        $headers = Values::of(['Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json' ]);
        return $this->version->handleRequest('GET', $this->uri, [], [], $headers, "fetch");
    }

    /**
     * Fetch the KnowledgeBaseInstance
     *
     
     * @return KnowledgeBaseInstance Fetched KnowledgeBaseInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function fetch(): KnowledgeBaseInstance
    {
        $response = $this->_fetch();
        return new KnowledgeBaseInstance(
            $this->version,
            $response->getContent(),
            $this->solution['kbId']
        );
        
    }

    /**
     * Fetch the KnowledgeBaseInstance with Metadata
     *
     
     * @return ResourceMetadata The Fetched Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function fetchWithMetadata(): ResourceMetadata
    {
        $response = $this->_fetch();
        $resource = new KnowledgeBaseInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['kbId']
                    );
        
        return new ResourceMetadata(
            $resource,
            $response->getStatusCode(),
            $response->getHeaders()
        );
    }


    /**
     * Helper function for Update
     *
     
     * @param UpdateKnowledgeBaseRequest $updateKnowledgeBaseRequest
     
     * @param array|Options $options Optional Arguments
     * @return Response Updated Response
     * @throws TwilioException When an HTTP error occurs.
     */
    private function _update(UpdateKnowledgeBaseRequest $updateKnowledgeBaseRequest, array $options = []): Response
    {
        
        $options = new Values($options);

        $headers = Values::of(['Content-Type' => 'application/json', 'Accept' => 'application/json' , 'If-Match' => $options['ifMatch']]);
        $data = $updateKnowledgeBaseRequest->toArray();
        return $this->version->handleRequest('PATCH', $this->uri, [], $data, $headers, "update");
    }

    /**
     * Update the UpdateKnowledgeBase202ResponseInstance
     *
     
     * @param UpdateKnowledgeBaseRequest $updateKnowledgeBaseRequest
     
     * @param array|Options $options Optional Arguments
     * @return UpdateKnowledgeBase202ResponseInstance Updated UpdateKnowledgeBase202ResponseInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function update(UpdateKnowledgeBaseRequest $updateKnowledgeBaseRequest, array $options = []): UpdateKnowledgeBase202ResponseInstance
    {
        $response = $this->_update( $updateKnowledgeBaseRequest, $options);
        return new UpdateKnowledgeBase202ResponseInstance(
            $this->version,
            $response->getContent(),
            $this->solution['kbId']
        );
        
    }

    /**
     * Update the UpdateKnowledgeBase202ResponseInstance with Metadata
     *
     
     * @param UpdateKnowledgeBaseRequest $updateKnowledgeBaseRequest
     
     * @param array|Options $options Optional Arguments
     * @return ResourceMetadata The Updated Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function updateWithMetadata(UpdateKnowledgeBaseRequest $updateKnowledgeBaseRequest, array $options = []): ResourceMetadata
    {
        $response = $this->_update( $updateKnowledgeBaseRequest, $options);
        $resource = new UpdateKnowledgeBase202ResponseInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['kbId']
                    );
        
        return new ResourceMetadata(
            $resource,
            $response->getStatusCode(),
            $response->getHeaders()
        );
    }


    /**
     * Provide a friendly representation
     *
     * @return string Machine friendly representation
     */
    public function __toString(): string
    {
        $context = [];
        foreach ($this->solution as $key => $value) {
            $context[] = "$key=$value";
        }
        return '[Twilio.Knowledge.V2.KnowledgeBasisContext ' . \implode(' ', $context) . ']';
    }
}
