using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.Wechat.OpenAI
{
    public static class WechatOpenAIThirdPartyClientExecuteOpenApiExtensions
    {
        /// <summary>
        /// <para>异步调用 [POST] /v1/openapi/create 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/doc/aispeech/openapi/api/v1/openapi/create.html </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.ThirdParty.OpenApiCreateResponse> ExecuteOpenApiCreateAsync(this WechatOpenAIThirdPartyClient client, Models.ThirdParty.OpenApiCreateRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Post, "v1", "openapi", "create");

            return await client.SendRequestWithJsonAsync<Models.ThirdParty.OpenApiCreateResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// <para>异步调用 [POST] /v1/openapi/reset 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/doc/aispeech/openapi/api/v1/openapi/reset.html </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.ThirdParty.OpenApiResetResponse> ExecuteOpenApiResetAsync(this WechatOpenAIThirdPartyClient client, Models.ThirdParty.OpenApiResetRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Post, "v1", "openapi", "reset");

            return await client.SendRequestWithJsonAsync<Models.ThirdParty.OpenApiResetResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// <para>异步调用 [GET] /v1/openapi/get_detail 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/doc/aispeech/openapi/api/v1/openapi/get_detail.html </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.ThirdParty.OpenApiGetDetailResponse> ExecuteOpenApiGetDetailAsync(this WechatOpenAIThirdPartyClient client, Models.ThirdParty.OpenApiGetDetailRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Get, "v1", "openapi", "get_detail");

            return await client.SendRequestWithJsonAsync<Models.ThirdParty.OpenApiGetDetailResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }
    }
}
