/*******************************************************************************
 *  Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License"). You may not use
 *  this file except in compliance with the License. A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 *  or in the "license" file accompanying this file.
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations under the License.
 * *****************************************************************************
 *
 *  AWS Tools for Windows (TM) PowerShell (TM)
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using Amazon.PowerShell.Common;
using Amazon.Runtime;
using Amazon.Kendra;
using Amazon.Kendra.Model;
namespace Amazon.PowerShell.Cmdlets.KNDR
{
    /// 
    /// Searches an index given an input query.
    /// 
    ///  
    /// 
    /// You can configure boosting or relevance tuning at the query level to override boosting
    /// at the index level, filter based on document fields/attributes and faceted search,
    /// and filter based on the user or their group access to documents. You can also include
    /// certain fields in the response that might provide useful additional information.
    /// 
    /// A query response contains three types of results.
    /// - 
    /// Relevant suggested answers. The answers can be either a text excerpt or table excerpt.
    /// The answer can be highlighted in the excerpt.
    /// 
 - 
    /// Matching FAQs or questions-answer from your FAQ file.
    /// 
 - 
    /// Relevant documents. This result type includes an excerpt of the document with the
    /// document title. The searched terms can be highlighted in the excerpt.
    /// 
 
    /// You can specify that the query return only one type of result using the QueryResultTypeFilter
    /// parameter. Each query returns the 100 most relevant results. If you filter result
    /// type to only question-answers, a maximum of four results are returned. If you filter
    /// result type to only answers, a maximum of three results are returned.
    /// 
    /// 
    [Cmdlet("Invoke", "KNDRQuery", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
    [OutputType("Amazon.Kendra.Model.QueryResponse")]
    [AWSCmdlet("Calls the Amazon Kendra Query API operation.", Operation = new[] {"Query"}, SelectReturnType = typeof(Amazon.Kendra.Model.QueryResponse))]
    [AWSCmdletOutput("Amazon.Kendra.Model.QueryResponse",
        "This cmdlet returns an Amazon.Kendra.Model.QueryResponse object containing multiple properties. The object can also be referenced from properties attached to the cmdlet entry in the $AWSHistory stack."
    )]
    public partial class InvokeKNDRQueryCmdlet : AmazonKendraClientCmdlet, IExecutor
    {
        
        #region Parameter AttributeFilter
        /// 
        /// 
        /// Filters search results by document fields/attributes. You can only provide one attribute
        /// filter; however, the AndAllFilters, NotFilter, and OrAllFilters
        /// parameters contain a list of other filters.The AttributeFilter parameter means you can create a set of filtering
        /// rules that a document must satisfy to be included in the query results.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public Amazon.Kendra.Model.AttributeFilter AttributeFilter { get; set; }
        #endregion
        
        #region Parameter UserContext_DataSourceGroup
        /// 
        /// 
        /// The list of data source groups you want to filter search results based on groups'
        /// access to documents in that data source.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("UserContext_DataSourceGroups")]
        public Amazon.Kendra.Model.DataSourceGroup[] UserContext_DataSourceGroup { get; set; }
        #endregion
        
        #region Parameter SortingConfiguration_DocumentAttributeKey
        /// 
        /// 
        /// The name of the document attribute used to sort the response. You can use any field
        /// that has the Sortable flag set to true.You can also sort by any of the following built-in attributes:- _category
 - _created_at
 - _last_updated_at
 - _version
 - _view_count
 
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public System.String SortingConfiguration_DocumentAttributeKey { get; set; }
        #endregion
        
        #region Parameter DocumentRelevanceOverrideConfiguration
        /// 
        /// 
        /// Overrides relevance tuning configurations of fields/attributes set at the index level.If you use this API to override the relevance tuning configured at the index level,
        /// but there is no relevance tuning configured at the index level, then Amazon Kendra
        /// does not apply any relevance tuning.If there is relevance tuning configured for fields at the index level, and you use
        /// this API to override only some of these fields, then for the fields you did not override,
        /// the importance is set to 1.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("DocumentRelevanceOverrideConfigurations")]
        public Amazon.Kendra.Model.DocumentRelevanceConfiguration[] DocumentRelevanceOverrideConfiguration { get; set; }
        #endregion
        
        #region Parameter Facet
        /// 
        /// 
        /// An array of documents fields/attributes for faceted search. Amazon Kendra returns
        /// a count for each field key specified. This helps your users narrow their search.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("Facets")]
        public Amazon.Kendra.Model.Facet[] Facet { get; set; }
        #endregion
        
        #region Parameter UserContext_Group
        /// 
        /// 
        /// The list of groups you want to filter search results based on the groups' access to
        /// documents.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("UserContext_Groups")]
        public System.String[] UserContext_Group { get; set; }
        #endregion
        
        #region Parameter SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion
        /// 
        /// 
        /// TRUE to suggest spell corrections for queries.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestions")]
        public System.Boolean? SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion { get; set; }
        #endregion
        
        #region Parameter IndexId
        /// 
        /// 
        /// The identifier of the index for the search.
        /// 
        /// 
        #if !MODULAR
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        #else
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true)]
        [System.Management.Automation.AllowEmptyString]
        [System.Management.Automation.AllowNull]
        #endif
        [Amazon.PowerShell.Common.AWSRequiredParameter]
        public System.String IndexId { get; set; }
        #endregion
        
        #region Parameter PageNumber
        /// 
        /// 
        /// Query results are returned in pages the size of the PageSize parameter.
        /// By default, Amazon Kendra returns the first page of results. Use this parameter to
        /// get result pages after the first one.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public System.Int32? PageNumber { get; set; }
        #endregion
        
        #region Parameter QueryResultTypeFilter
        /// 
        /// 
        /// Sets the type of query result or response. Only results for the specified type are
        /// returned.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [AWSConstantClassSource("Amazon.Kendra.QueryResultType")]
        public Amazon.Kendra.QueryResultType QueryResultTypeFilter { get; set; }
        #endregion
        
        #region Parameter QueryText
        /// 
        /// 
        /// The input query text for the search. Amazon Kendra truncates queries at 30 token words,
        /// which excludes punctuation and stop words. Truncation still applies if you use Boolean
        /// or more advanced, complex queries. 
        /// 
        /// 
        [System.Management.Automation.Parameter(Position = 0, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
        public System.String QueryText { get; set; }
        #endregion
        
        #region Parameter RequestedDocumentAttribute
        /// 
        /// 
        /// An array of document fields/attributes to include in the response. You can limit the
        /// response to include certain document fields. By default, all document attributes are
        /// included in the response.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [Alias("RequestedDocumentAttributes")]
        public System.String[] RequestedDocumentAttribute { get; set; }
        #endregion
        
        #region Parameter SortingConfiguration_SortOrder
        /// 
        /// 
        /// The order that the results should be returned in. In case of ties, the relevance assigned
        /// to the result by Amazon Kendra is used as the tie-breaker.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        [AWSConstantClassSource("Amazon.Kendra.SortOrder")]
        public Amazon.Kendra.SortOrder SortingConfiguration_SortOrder { get; set; }
        #endregion
        
        #region Parameter UserContext_Token
        /// 
        /// 
        /// The user context token for filtering search results for a user. It must be a JWT or
        /// a JSON token.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public System.String UserContext_Token { get; set; }
        #endregion
        
        #region Parameter UserContext_UserId
        /// 
        /// 
        /// The identifier of the user you want to filter search results based on their access
        /// to documents.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public System.String UserContext_UserId { get; set; }
        #endregion
        
        #region Parameter VisitorId
        /// 
        /// 
        /// Provides an identifier for a specific user. The VisitorId should be a
        /// unique identifier, such as a GUID. Don't use personally identifiable information,
        /// such as the user's email address, as the VisitorId.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public System.String VisitorId { get; set; }
        #endregion
        
        #region Parameter PageSize
        /// 
        /// 
        /// Sets the number of results that are returned in each page of results. The default
        /// page size is 10. The maximum number of results returned is 100. If you ask for more
        /// than 100 results, only 100 are returned.
        /// 
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public System.Int32? PageSize { get; set; }
        #endregion
        
        #region Parameter Select
        /// 
        /// Use the -Select parameter to control the cmdlet output. The default value is '*'.
        /// Specifying -Select '*' will result in the cmdlet returning the whole service response (Amazon.Kendra.Model.QueryResponse).
        /// Specifying the name of a property of type Amazon.Kendra.Model.QueryResponse will result in that property being returned.
        /// Specifying -Select '^ParameterName' will result in the cmdlet returning the selected cmdlet parameter value.
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public string Select { get; set; } = "*";
        #endregion
        
        #region Parameter PassThru
        /// 
        /// Changes the cmdlet behavior to return the value passed to the QueryText parameter.
        /// The -PassThru parameter is deprecated, use -Select '^QueryText' instead. This parameter will be removed in a future version.
        /// 
        [System.Obsolete("The -PassThru parameter is deprecated, use -Select '^QueryText' instead. This parameter will be removed in a future version.")]
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public SwitchParameter PassThru { get; set; }
        #endregion
        
        #region Parameter Force
        /// 
        /// This parameter overrides confirmation prompts to force 
        /// the cmdlet to continue its operation. This parameter should always
        /// be used with caution.
        /// 
        [System.Management.Automation.Parameter(ValueFromPipelineByPropertyName = true)]
        public SwitchParameter Force { get; set; }
        #endregion
        
        protected override void ProcessRecord()
        {
            this._AWSSignerType = "v4";
            base.ProcessRecord();
            
            var resourceIdentifiersText = FormatParameterValuesForConfirmationMsg(nameof(this.IndexId), MyInvocation.BoundParameters);
            if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Invoke-KNDRQuery (Query)"))
            {
                return;
            }
            
            var context = new CmdletContext();
            
            // allow for manipulation of parameters prior to loading into context
            PreExecutionContextLoad(context);
            
            #pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
            if (ParameterWasBound(nameof(this.Select)))
            {
                context.Select = CreateSelectDelegate(Select) ??
                    throw new System.ArgumentException("Invalid value for -Select parameter.", nameof(this.Select));
                if (this.PassThru.IsPresent)
                {
                    throw new System.ArgumentException("-PassThru cannot be used when -Select is specified.", nameof(this.Select));
                }
            }
            else if (this.PassThru.IsPresent)
            {
                context.Select = (response, cmdlet) => this.QueryText;
            }
            #pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
            context.AttributeFilter = this.AttributeFilter;
            if (this.DocumentRelevanceOverrideConfiguration != null)
            {
                context.DocumentRelevanceOverrideConfiguration = new List(this.DocumentRelevanceOverrideConfiguration);
            }
            if (this.Facet != null)
            {
                context.Facet = new List(this.Facet);
            }
            context.IndexId = this.IndexId;
            #if MODULAR
            if (this.IndexId == null && ParameterWasBound(nameof(this.IndexId)))
            {
                WriteWarning("You are passing $null as a value for parameter IndexId which is marked as required. In case you believe this parameter was incorrectly marked as required, report this by opening an issue at https://github.com/aws/aws-tools-for-powershell/issues.");
            }
            #endif
            context.PageNumber = this.PageNumber;
            context.PageSize = this.PageSize;
            context.QueryResultTypeFilter = this.QueryResultTypeFilter;
            context.QueryText = this.QueryText;
            if (this.RequestedDocumentAttribute != null)
            {
                context.RequestedDocumentAttribute = new List(this.RequestedDocumentAttribute);
            }
            context.SortingConfiguration_DocumentAttributeKey = this.SortingConfiguration_DocumentAttributeKey;
            context.SortingConfiguration_SortOrder = this.SortingConfiguration_SortOrder;
            context.SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion = this.SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion;
            if (this.UserContext_DataSourceGroup != null)
            {
                context.UserContext_DataSourceGroup = new List(this.UserContext_DataSourceGroup);
            }
            if (this.UserContext_Group != null)
            {
                context.UserContext_Group = new List(this.UserContext_Group);
            }
            context.UserContext_Token = this.UserContext_Token;
            context.UserContext_UserId = this.UserContext_UserId;
            context.VisitorId = this.VisitorId;
            
            // allow further manipulation of loaded context prior to processing
            PostExecutionContextLoad(context);
            
            var output = Execute(context) as CmdletOutput;
            ProcessOutput(output);
        }
        
        #region IExecutor Members
        
        public object Execute(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;
            // create request
            var request = new Amazon.Kendra.Model.QueryRequest();
            
            if (cmdletContext.AttributeFilter != null)
            {
                request.AttributeFilter = cmdletContext.AttributeFilter;
            }
            if (cmdletContext.DocumentRelevanceOverrideConfiguration != null)
            {
                request.DocumentRelevanceOverrideConfigurations = cmdletContext.DocumentRelevanceOverrideConfiguration;
            }
            if (cmdletContext.Facet != null)
            {
                request.Facets = cmdletContext.Facet;
            }
            if (cmdletContext.IndexId != null)
            {
                request.IndexId = cmdletContext.IndexId;
            }
            if (cmdletContext.PageNumber != null)
            {
                request.PageNumber = cmdletContext.PageNumber.Value;
            }
            if (cmdletContext.PageSize != null)
            {
                request.PageSize = cmdletContext.PageSize.Value;
            }
            if (cmdletContext.QueryResultTypeFilter != null)
            {
                request.QueryResultTypeFilter = cmdletContext.QueryResultTypeFilter;
            }
            if (cmdletContext.QueryText != null)
            {
                request.QueryText = cmdletContext.QueryText;
            }
            if (cmdletContext.RequestedDocumentAttribute != null)
            {
                request.RequestedDocumentAttributes = cmdletContext.RequestedDocumentAttribute;
            }
            
             // populate SortingConfiguration
            var requestSortingConfigurationIsNull = true;
            request.SortingConfiguration = new Amazon.Kendra.Model.SortingConfiguration();
            System.String requestSortingConfiguration_sortingConfiguration_DocumentAttributeKey = null;
            if (cmdletContext.SortingConfiguration_DocumentAttributeKey != null)
            {
                requestSortingConfiguration_sortingConfiguration_DocumentAttributeKey = cmdletContext.SortingConfiguration_DocumentAttributeKey;
            }
            if (requestSortingConfiguration_sortingConfiguration_DocumentAttributeKey != null)
            {
                request.SortingConfiguration.DocumentAttributeKey = requestSortingConfiguration_sortingConfiguration_DocumentAttributeKey;
                requestSortingConfigurationIsNull = false;
            }
            Amazon.Kendra.SortOrder requestSortingConfiguration_sortingConfiguration_SortOrder = null;
            if (cmdletContext.SortingConfiguration_SortOrder != null)
            {
                requestSortingConfiguration_sortingConfiguration_SortOrder = cmdletContext.SortingConfiguration_SortOrder;
            }
            if (requestSortingConfiguration_sortingConfiguration_SortOrder != null)
            {
                request.SortingConfiguration.SortOrder = requestSortingConfiguration_sortingConfiguration_SortOrder;
                requestSortingConfigurationIsNull = false;
            }
             // determine if request.SortingConfiguration should be set to null
            if (requestSortingConfigurationIsNull)
            {
                request.SortingConfiguration = null;
            }
            
             // populate SpellCorrectionConfiguration
            var requestSpellCorrectionConfigurationIsNull = true;
            request.SpellCorrectionConfiguration = new Amazon.Kendra.Model.SpellCorrectionConfiguration();
            System.Boolean? requestSpellCorrectionConfiguration_spellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion = null;
            if (cmdletContext.SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion != null)
            {
                requestSpellCorrectionConfiguration_spellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion = cmdletContext.SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion.Value;
            }
            if (requestSpellCorrectionConfiguration_spellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion != null)
            {
                request.SpellCorrectionConfiguration.IncludeQuerySpellCheckSuggestions = requestSpellCorrectionConfiguration_spellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion.Value;
                requestSpellCorrectionConfigurationIsNull = false;
            }
             // determine if request.SpellCorrectionConfiguration should be set to null
            if (requestSpellCorrectionConfigurationIsNull)
            {
                request.SpellCorrectionConfiguration = null;
            }
            
             // populate UserContext
            var requestUserContextIsNull = true;
            request.UserContext = new Amazon.Kendra.Model.UserContext();
            List requestUserContext_userContext_DataSourceGroup = null;
            if (cmdletContext.UserContext_DataSourceGroup != null)
            {
                requestUserContext_userContext_DataSourceGroup = cmdletContext.UserContext_DataSourceGroup;
            }
            if (requestUserContext_userContext_DataSourceGroup != null)
            {
                request.UserContext.DataSourceGroups = requestUserContext_userContext_DataSourceGroup;
                requestUserContextIsNull = false;
            }
            List requestUserContext_userContext_Group = null;
            if (cmdletContext.UserContext_Group != null)
            {
                requestUserContext_userContext_Group = cmdletContext.UserContext_Group;
            }
            if (requestUserContext_userContext_Group != null)
            {
                request.UserContext.Groups = requestUserContext_userContext_Group;
                requestUserContextIsNull = false;
            }
            System.String requestUserContext_userContext_Token = null;
            if (cmdletContext.UserContext_Token != null)
            {
                requestUserContext_userContext_Token = cmdletContext.UserContext_Token;
            }
            if (requestUserContext_userContext_Token != null)
            {
                request.UserContext.Token = requestUserContext_userContext_Token;
                requestUserContextIsNull = false;
            }
            System.String requestUserContext_userContext_UserId = null;
            if (cmdletContext.UserContext_UserId != null)
            {
                requestUserContext_userContext_UserId = cmdletContext.UserContext_UserId;
            }
            if (requestUserContext_userContext_UserId != null)
            {
                request.UserContext.UserId = requestUserContext_userContext_UserId;
                requestUserContextIsNull = false;
            }
             // determine if request.UserContext should be set to null
            if (requestUserContextIsNull)
            {
                request.UserContext = null;
            }
            if (cmdletContext.VisitorId != null)
            {
                request.VisitorId = cmdletContext.VisitorId;
            }
            
            CmdletOutput output;
            
            // issue call
            var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);
            try
            {
                var response = CallAWSServiceOperation(client, request);
                object pipelineOutput = null;
                pipelineOutput = cmdletContext.Select(response, this);
                output = new CmdletOutput
                {
                    PipelineOutput = pipelineOutput,
                    ServiceResponse = response
                };
            }
            catch (Exception e)
            {
                output = new CmdletOutput { ErrorResponse = e };
            }
            
            return output;
        }
        
        public ExecutorContext CreateContext()
        {
            return new CmdletContext();
        }
        
        #endregion
        
        #region AWS Service Operation Call
        
        private Amazon.Kendra.Model.QueryResponse CallAWSServiceOperation(IAmazonKendra client, Amazon.Kendra.Model.QueryRequest request)
        {
            Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Kendra", "Query");
            try
            {
                #if DESKTOP
                return client.Query(request);
                #elif CORECLR
                return client.QueryAsync(request).GetAwaiter().GetResult();
                #else
                        #error "Unknown build edition"
                #endif
            }
            catch (AmazonServiceException exc)
            {
                var webException = exc.InnerException as System.Net.WebException;
                if (webException != null)
                {
                    throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
                }
                throw;
            }
        }
        
        #endregion
        
        internal partial class CmdletContext : ExecutorContext
        {
            public Amazon.Kendra.Model.AttributeFilter AttributeFilter { get; set; }
            public List DocumentRelevanceOverrideConfiguration { get; set; }
            public List Facet { get; set; }
            public System.String IndexId { get; set; }
            public System.Int32? PageNumber { get; set; }
            public System.Int32? PageSize { get; set; }
            public Amazon.Kendra.QueryResultType QueryResultTypeFilter { get; set; }
            public System.String QueryText { get; set; }
            public List RequestedDocumentAttribute { get; set; }
            public System.String SortingConfiguration_DocumentAttributeKey { get; set; }
            public Amazon.Kendra.SortOrder SortingConfiguration_SortOrder { get; set; }
            public System.Boolean? SpellCorrectionConfiguration_IncludeQuerySpellCheckSuggestion { get; set; }
            public List UserContext_DataSourceGroup { get; set; }
            public List UserContext_Group { get; set; }
            public System.String UserContext_Token { get; set; }
            public System.String UserContext_UserId { get; set; }
            public System.String VisitorId { get; set; }
            public System.Func Select { get; set; } =
                (response, cmdlet) => response;
        }
        
    }
}