// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Teams.Common.Http;

namespace Microsoft.Teams.Api.Clients;

public class BotSignInClient : Client
{
    public string TokenServiceUrl { get; set; } = "https://token.botframework.com";

    public BotSignInClient() : base()
    {

    }

    public BotSignInClient(IHttpClient client, CancellationToken cancellationToken = default) : base(client, cancellationToken)
    {

    }

    public BotSignInClient(IHttpClientOptions options, CancellationToken cancellationToken = default) : base(options, cancellationToken)
    {

    }

    public BotSignInClient(IHttpClientFactory factory, CancellationToken cancellationToken = default) : base(factory, cancellationToken)
    {

    }

    public async Task<string> GetUrlAsync(GetUrlRequest request, CancellationToken cancellationToken = default)
    {
        var token = cancellationToken != default ? cancellationToken : _cancellationToken;
        var query = QueryString.Serialize(request);
        var req = HttpRequest.Get(
            $"{TokenServiceUrl}/api/botsignin/GetSignInUrl?{query}"
        );

        var res = await _http.SendAsync(req, token).ConfigureAwait(false);
        return res.Body;
    }

    public async Task<SignIn.UrlResponse> GetResourceAsync(GetResourceRequest request, CancellationToken cancellationToken = default)
    {
        var token = cancellationToken != default ? cancellationToken : _cancellationToken;
        var query = QueryString.Serialize(request);
        var req = HttpRequest.Get(
            $"{TokenServiceUrl}/api/botsignin/GetSignInResource?{query}"
        );

        var res = await _http.SendAsync<SignIn.UrlResponse>(req, token).ConfigureAwait(false);
        return res.Body;
    }

    public class GetUrlRequest
    {
        public required string State { get; set; }
        public string? CodeChallenge { get; set; }
        public string? EmulatorUrl { get; set; }
        public string? FinalRedirect { get; set; }
    }

    public class GetResourceRequest
    {
        public required string State { get; set; }
        public string? CodeChallenge { get; set; }
        public string? EmulatorUrl { get; set; }
        public string? FinalRedirect { get; set; }
    }
}