/// <summary>
        /// Creates a new instance of an UnexpectedStatusCodeError.
        /// </summary>
        /// <param name="response">The HttpResponseMessage to convert to an error.</param>
        /// <param name="target">The target of the error.</param>
        /// <returns>The new UnexpectedStatusCodeError.</returns>
        public static async Task <UnexpectedStatusCodeError> CreateAsync(HttpResponseMessage response, string target)
        {
            if (response is null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            var error = new UnexpectedStatusCodeError();

            error.Code    = "UnexpectedStatusCode";
            error.Target  = target;
            error.Message = $"The HTTP status code of the response was not expected ({(int)response.StatusCode}).";

            if (response.Content != null)
            {
                var stringContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                error.Content = stringContent;
            }

            error.ReasonPhrase = response.ReasonPhrase;
            error.StatusCode   = response.StatusCode;

            return(error);
        }
        /// <summary>
        /// Gets a particular form submission attachment.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="submissionId">The ID of the requested submission.</param>
        /// <param name="attachmentName">The name of the attachment.</param>
        /// <param name="tenantId">The ID of the requested tenant.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The result or an error.</returns>
        public static async Task <Result <HttpFile> > GetSubmissionAttachmentResultAsync(this IFormsClient formsClient, Guid formId, Guid submissionId, string attachmentName, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            HttpResponseMessage response = await formsClient.GetSubmissionAttachmentHttpResponseAsync(formId, submissionId, attachmentName, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(new HttpFile
                    {
                        Data = await response.Content.ReadAsStreamAsync().ConfigureAwait(false),
                        ContentType = response.Content?.Headers?.ContentType?.MediaType,
                        FileName = response.Content?.Headers?.ContentDisposition?.FileName,
                    });

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult <HttpFile>());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IFormsClient)}.{nameof(CreateFormResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult <HttpFile>());
                }
                }
            }
        }
        /// <summary>
        /// Creates a new form specific webhook.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="request">The request body.</param>
        /// <param name="tenantId">The ID of the requested tenant.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The result or an error.</returns>
        public static async Task <Result <Webhook> > CreateWebhookResultAsync(this IFormsClient formsClient, Guid formId, CreateWebhookRequest request, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            HttpResponseMessage response = await formsClient.CreateWebhookHttpResponseAsync(formId, request, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Create(await response.DeserializeJsonContentAsync <Webhook>().ConfigureAwait(false)));

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult <Webhook>());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IFormsClient)}.{nameof(CreateFormResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult <Webhook>());
                }
                }
            }
        }
        /// <summary>
        /// Submits a new form to VendorHub.
        /// </summary>
        /// <param name="formsClient">The IFormsClient to use.</param>
        /// <param name="formId">The ID of the requested form.</param>
        /// <param name="fields">The text fields to submit.</param>
        /// <param name="attachments">The files to attach.</param>
        /// <param name="tenantId">The ID of the requested tenant.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The result or an error.</returns>
        public static async Task <Result <FormSubmission> > SubmitFormResultAsync(this IFormsClient formsClient, Guid formId, IEnumerable <KeyValuePair <string, string> > fields, IEnumerable <KeyValuePair <string, HttpFile> > attachments, Guid tenantId, CancellationToken cancellationToken = default)
        {
            if (formsClient is null)
            {
                throw new ArgumentNullException(nameof(formsClient));
            }

            HttpResponseMessage response = await formsClient.SubmitFormHttpResponseAsync(formId, fields, attachments, tenantId, cancellationToken).ConfigureAwait(false);

            using (response)
            {
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(Result.Create(await response.DeserializeJsonContentAsync <FormSubmission>().ConfigureAwait(false)));

                case HttpStatusCode.NoContent:
                    return(default);

                case HttpStatusCode.BadRequest:
                case HttpStatusCode.InternalServerError:
                {
                    ErrorResponse errorResponse = await response.DeserializeJsonContentAsync <ErrorResponse>().ConfigureAwait(false);

                    return(errorResponse.Error.ToResult <FormSubmission>());
                }

                default:
                {
                    UnexpectedStatusCodeError error = await UnexpectedStatusCodeError.CreateAsync(response, $"{nameof(IFormsClient)}.{nameof(CreateFormResultAsync)}").ConfigureAwait(false);

                    return(error.ToResult <FormSubmission>());
                }
                }
            }
        }