<?php

/**
 * This code was generated by
 * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
 *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
 *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
 *
 * Conversation Orchestrator
 * Manage configurations, conversations, participants, and communications. Create configurations to define capture rules and channel settings, then use conversations to group related communications.
 *
 * NOTE: This class is auto generated by OpenAPI Generator.
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


namespace Twilio\Rest\Conversations\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 ConfigurationContext extends InstanceContext
    {
    /**
     * Initialize the ConfigurationContext
     *
     * @param Version $version Version that contains the resource
     * @param string $sid
     */
    public function __construct(
        Version $version,
        $sid
    ) {
        $apiV1Version = new ApiV1Version($version->getDomain(), $version->version);
        parent::__construct($apiV1Version);

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

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

    /**
     * Helper function for Delete
     *
     
     * @param array|Options $options Optional Arguments
     * @return Response Deleted Response
     * @throws TwilioException When an HTTP error occurs.
     */
    private function _delete(array $options = []): Response
    {
        
        $options = new Values($options);

        $headers = Values::of(['Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json' , 'Idempotency-Key' => $options['idempotencyKey']]);
        return $this->version->handleRequest('DELETE', $this->uri, [], [], $headers, "delete");
    }

    /**
     * Delete the CreateConfiguration202ResponseInstance
     *
     
     * @param array|Options $options Optional Arguments
     * @return CreateConfiguration202ResponseInstance Deleted CreateConfiguration202ResponseInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function delete(array $options = []): CreateConfiguration202ResponseInstance
    {
        $response = $this->_delete($options);
        
        return new CreateConfiguration202ResponseInstance(
            $this->version,
            $response->getContent(),
            $this->solution['sid']
        );
    }

    /**
     * Delete the CreateConfiguration202ResponseInstance with Metadata
     *
     
     * @param array|Options $options Optional Arguments
     * @return ResourceMetadata The Deleted Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function deleteWithMetadata(array $options = []): ResourceMetadata
    {
        $response = $this->_delete($options);
        
        $resource = new CreateConfiguration202ResponseInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['sid']
                    );
        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 ListConfiguration200ResponseConfigurationsInstance
     *
     
     * @return ListConfiguration200ResponseConfigurationsInstance Fetched ListConfiguration200ResponseConfigurationsInstance
     * @throws TwilioException When an HTTP error occurs.
     */
    public function fetch(): ListConfiguration200ResponseConfigurationsInstance
    {
        $response = $this->_fetch();
        return new ListConfiguration200ResponseConfigurationsInstance(
            $this->version,
            $response->getContent(),
            $this->solution['sid']
        );
        
    }

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


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

        $headers = Values::of(['Content-Type' => 'application/json', 'Accept' => 'application/json' , 'Idempotency-Key' => $options['idempotencyKey']]);
        $data = $updateConfigurationRequest ? $updateConfigurationRequest->toArray() : [];
        return $this->version->handleRequest('PUT', $this->uri, [], $data, $headers, "update");
    }

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

    /**
     * Update the CreateConfiguration202ResponseInstance with Metadata
     *
     
     * @param ?UpdateConfigurationRequest $updateConfigurationRequest The configuration to update
     * @param array|Options $options Optional Arguments
     * @return ResourceMetadata The Updated Resource with Metadata
     * @throws TwilioException When an HTTP error occurs.
     */
    public function updateWithMetadata(?UpdateConfigurationRequest $updateConfigurationRequest = null, array $options = []): ResourceMetadata
    {
        $response = $this->_update($updateConfigurationRequest, $options);
        $resource = new CreateConfiguration202ResponseInstance(
                        $this->version,
                        $response->getContent(),
                        $this->solution['sid']
                    );
        
        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.Conversations.V2.ConfigurationContext ' . \implode(' ', $context) . ']';
    }
}
