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

using Microsoft.Teams.Apps.Schema;
using Microsoft.Teams.Core;
using Microsoft.Teams.Core.Schema;

using CoreConversationClient = Microsoft.Teams.Core.ConversationClient;

namespace Microsoft.Teams.Apps.Api.Clients;

/// <summary>
/// Client for managing conversation members.
/// Delegates to the core <see cref="CoreConversationClient"/>.
/// </summary>
public class MemberClient
{
    private readonly CoreConversationClient _client;
    private readonly Uri _serviceUrl;

    internal MemberClient(Uri serviceUrl, CoreConversationClient client)
    {
        _serviceUrl = serviceUrl;
        _client = client;
    }

    /// <summary>
    /// Get all members of a conversation.
    /// </summary>
    [Obsolete("Use GetPagedAsync instead.")]
    public async Task<IList<TeamsConversationAccount?>> GetAsync(string conversationId, AgenticIdentity? agenticIdentity = null, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default)
    {
        IList<ConversationAccount> members = await _client.GetConversationMembersAsync(conversationId, _serviceUrl, agenticIdentity: agenticIdentity, customHeaders: additionalHeaders, cancellationToken: cancellationToken).ConfigureAwait(false);
        return [.. members.Select(m => TeamsConversationAccount.FromConversationAccount(m))];
    }

    /// <summary>
    /// Get members of a conversation with pagination support.
    /// </summary>
    public async Task<PagedTeamsMembersResult> GetPagedAsync(
        string conversationId,
        int pageSize = 50,
        string? continuationToken = null,
        AgenticIdentity? agenticIdentity = null,
        Dictionary<string, string>? additionalHeaders = null,
        CancellationToken cancellationToken = default)
    {
        PagedMembersResult? paged = await _client.GetConversationPagedMembersAsync(
            conversationId,
            _serviceUrl,
            pageSize,
            continuationToken,
            agenticIdentity: agenticIdentity,
            customHeaders: additionalHeaders,
            cancellationToken: cancellationToken).ConfigureAwait(false);
        PagedTeamsMembersResult result = new();
        if (paged is not null)
        {
            result.ContinuationToken = paged.ContinuationToken;
            if (paged.Members is not null)
            {
                result.Members = [.. paged.Members.Select(m => TeamsConversationAccount.FromConversationAccount(m))];
            }
        }
        return result;
    }

    /// <summary>
    /// Get a specific member of a conversation by ID.
    /// </summary>
    public Task<T> GetByIdAsync<T>(string conversationId, string memberId, AgenticIdentity? agenticIdentity = null, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default) where T : ConversationAccount
    {
        return _client.GetConversationMemberAsync<T>(conversationId, memberId, _serviceUrl, agenticIdentity: agenticIdentity, customHeaders: additionalHeaders, cancellationToken: cancellationToken);
    }

    /// <summary>
    /// Get a specific member of a conversation by ID.
    /// </summary>
    public async Task<TeamsConversationAccount?> GetByIdAsync(string conversationId, string memberId, AgenticIdentity? agenticIdentity = null, Dictionary<string, string>? additionalHeaders = null, CancellationToken cancellationToken = default)
    {
        ConversationAccount member = await GetByIdAsync<ConversationAccount>(conversationId, memberId, agenticIdentity, additionalHeaders, cancellationToken).ConfigureAwait(false);
        return TeamsConversationAccount.FromConversationAccount(member);
    }
}
