<?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;
use Twilio\Serialize;


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

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

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

    /**
     * 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 Instance
     *
     
     
     * @return bool True if delete succeeds, false otherwise
     * @throws TwilioException When an HTTP error occurs.
     */
    public function delete(): bool
    {
        $response = $this->_delete();
        
        return true;
    }

    /**
     * Delete the Instance with Metadata
     *
     
     
     * @return ResourceMetadata The Deleted Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function deleteWithMetadata(): ResourceMetadata
    {
        $response = $this->_delete();
        
        
        return new ResourceMetadata(
            null,
            $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 KnowledgeInstance
     *
     
     
     * @return KnowledgeInstance Fetched KnowledgeInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function fetch(): KnowledgeInstance
    {
        $response = $this->_fetch();
        return new KnowledgeInstance(
            $this->version,
            $response->getContent(),
            $this->solution['kbId'],
            $this->solution['knowledgeId']
        );
        
    }

    /**
     * Fetch the KnowledgeInstance 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 KnowledgeInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['kbId'],
                        $this->solution['knowledgeId']
                    );
        
        return new ResourceMetadata(
            $resource,
            $response->getStatusCode(),
            $response->getHeaders()
        );
    }


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

        $params = Values::of([
            'refresh' =>
                Serialize::booleanToString($options['refresh']),
        ]);

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

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

    /**
     * Update the KnowledgeInstance with Metadata
     *
     
     
     * @param ?KnowledgeCore $knowledgeCore
     * @param array|Options $options Optional Arguments
     * @return ResourceMetadata The Updated Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function updateWithMetadata(?KnowledgeCore $knowledgeCore = null, array $options = []): ResourceMetadata
    {
        $response = $this->_update($knowledgeCore, $options);
        $resource = new KnowledgeInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['kbId'],
                        $this->solution['knowledgeId']
                    );
        
        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.KnowledgeContext ' . \implode(' ', $context) . ']';
    }
}
