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

using Microsoft.Teams.Api.Auth;

namespace Microsoft.Teams.Apps.Extensions;

public class TeamsSettings
{
    /// <summary>
    /// The ID assigned to your application.
    /// </summary>
    public string? ClientId { get; set; }

    /// <summary>
    /// The secret (i.e. password) for your application.
    /// </summary>
    public string? ClientSecret { get; set; }

    /// <summary>
    /// The Tenant ID assigned to your application (for single tenant apps only)
    /// </summary>
    public string? TenantId { get; set; }

    /// <summary>
    /// Url used for client to perform tab auth and link the NAA account to the bot login account.
    /// </summary>
    public string? AccountLinkingUrl { get; set; }

    /// <summary>
    /// True when <code>ClientId</code> OR <code>ClientSecret</code> are empty
    /// </summary>
    public bool Empty => string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret);

    /// <summary>
    /// Apply settings to app options.
    /// </summary>
    public AppOptions Apply(AppOptions? options = null)
    {
        options ??= new AppOptions();

        if (ClientId is not null && ClientSecret is not null && !Empty)
        {
            options.Credentials = new ClientCredentials(ClientId, ClientSecret, TenantId);
        }

        if (!string.IsNullOrEmpty(AccountLinkingUrl))
        {
            options.OAuth.AccountLinkingUrl = AccountLinkingUrl;
        }

        return options;
    }
}