<?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\InstanceResource;
use Twilio\Options;
use Twilio\Values;
use Twilio\Version;
use Twilio\ApiV1Version;
use Twilio\Deserialize;


/**
 * @property string $name
 * @property string $description
 * @property string $source
 * @property string $id
 * @property string $status
 * @property \DateTime $createdAt
 * @property \DateTime $updatedAt
 */
class KnowledgeInstance extends InstanceResource
{
    /**
     * Initialize the KnowledgeInstance
     *
     * @param Version $version Version that contains the resource
     * @param mixed[] $payload The response payload
     * @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, array $payload, ?string $kbId = null, ?string $knowledgeId = null)
    {
        $apiV1Version = new ApiV1Version($version->getDomain(), $version->version);
        parent::__construct($apiV1Version);

        // Marshaled Properties
        $this->properties = [
            'name' => Values::array_get($payload, 'name'),
            'description' => Values::array_get($payload, 'description'),
            'source' => Values::array_get($payload, 'source'),
            'id' => Values::array_get($payload, 'id'),
            'status' => Values::array_get($payload, 'status'),
            'createdAt' => Deserialize::dateTime(Values::array_get($payload, 'createdAt')),
            'updatedAt' => Deserialize::dateTime(Values::array_get($payload, 'updatedAt')),
        ];

        $this->solution = ['kbId' => $kbId ?: ($this->properties['kbId'] ?? null), 'knowledgeId' => $knowledgeId ?: ($this->properties['knowledgeId'] ?? null), ];
    }

    /**
     * Generate an instance context for the instance, the context is capable of
     * performing various actions.  All instance actions are proxied to the context
     *
     * @return KnowledgeContext Context for this KnowledgeInstance
     */
    protected function proxy(): KnowledgeContext
    {
        if (!$this->context) {
            $this->context = new KnowledgeContext(
                $this->version,
                $this->solution['kbId'],
                $this->solution['knowledgeId']
            );
        }

        return $this->context;
    }

    /**
     * Delete the Instance
     *
     * @return bool True if delete succeeds, false otherwise
     * @throws TwilioException When an HTTP error occurs.
     */
    public function delete(): bool
    {

        return $this->proxy()->delete();
    }

    /**
     * Fetch the KnowledgeInstance
     *
     * @return KnowledgeInstance Fetched KnowledgeInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function fetch(): KnowledgeInstance
    {

        return $this->proxy()->fetch();
    }

    /**
     * Update the KnowledgeInstance
     *
     * @param array|Options $options Optional Arguments
     * @return KnowledgeInstance Updated KnowledgeInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function update(array $options = []): KnowledgeInstance
    {

        return $this->proxy()->update($options);
    }

    /**
     * Magic getter to access properties
     *
     * @param string $name Property to access
     * @return mixed The requested property
     * @throws TwilioException For unknown properties
     */
    public function __get(string $name)
    {
        if (\array_key_exists($name, $this->properties)) {
            return $this->properties[$name];
        }

        if (\property_exists($this, '_' . $name)) {
            $method = 'get' . \ucfirst($name);
            return $this->$method();
        }

        throw new TwilioException('Unknown property: ' . $name);
    }

    /**
     * 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.KnowledgeInstance ' . \implode(' ', $context) . ']';
    }
}
