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

using System.Text.Json.Serialization;

using Microsoft.Teams.Api.Activities;
using Microsoft.Teams.Common.Http;

namespace Microsoft.Teams.Api.Clients;

public class ConversationClient : Client
{
    public readonly string ServiceUrl;
    public readonly ActivityClient Activities;
    public readonly MemberClient Members;
    public readonly ReactionClient Reactions;

    public ConversationClient(string serviceUrl, CancellationToken cancellationToken = default) : base(cancellationToken)
    {
        ServiceUrl = serviceUrl;
        Activities = new ActivityClient(serviceUrl, _http, cancellationToken);
        Members = new MemberClient(serviceUrl, _http, cancellationToken);
        Reactions = new ReactionClient(serviceUrl, _http, cancellationToken);
    }

    public ConversationClient(string serviceUrl, IHttpClient client, CancellationToken cancellationToken = default) : base(client, cancellationToken)
    {
        ServiceUrl = serviceUrl;
        Activities = new ActivityClient(serviceUrl, _http, cancellationToken);
        Members = new MemberClient(serviceUrl, _http, cancellationToken);
        Reactions = new ReactionClient(serviceUrl, _http, cancellationToken);
    }

    public ConversationClient(string serviceUrl, IHttpClientOptions options, CancellationToken cancellationToken = default) : base(options, cancellationToken)
    {
        ServiceUrl = serviceUrl;
        Activities = new ActivityClient(serviceUrl, _http, cancellationToken);
        Members = new MemberClient(serviceUrl, _http, cancellationToken);
        Reactions = new ReactionClient(serviceUrl, _http, cancellationToken);
    }

    public ConversationClient(string serviceUrl, IHttpClientFactory factory, CancellationToken cancellationToken = default) : base(factory, cancellationToken)
    {
        ServiceUrl = serviceUrl;
        Activities = new ActivityClient(serviceUrl, _http, cancellationToken);
        Members = new MemberClient(serviceUrl, _http, cancellationToken);
        Reactions = new ReactionClient(serviceUrl, _http, cancellationToken);
    }

    public async Task<ConversationResource> CreateAsync(CreateRequest request, CancellationToken cancellationToken = default)
    {
        var token = cancellationToken != default ? cancellationToken : _cancellationToken;
        var req = HttpRequest.Post($"{ServiceUrl}v3/conversations", body: request);
        var res = await _http.SendAsync<ConversationResource>(req, token);
        return res.Body;
    }

    public class CreateRequest
    {
        [JsonPropertyName("isGroup")]
        [JsonPropertyOrder(0)]
        [Obsolete("This will be removed by end of summer 2026.")]
        public bool? IsGroup { get; set; }

        [JsonPropertyName("bot")]
        [JsonPropertyOrder(1)]
        [Obsolete("This will be removed by end of summer 2026.")]
        public Account? Bot { get; set; }

        [JsonPropertyName("members")]
        [JsonPropertyOrder(2)]
        public IList<Account>? Members { get; set; }

        [JsonPropertyName("topicName")]
        [JsonPropertyOrder(3)]
        [Obsolete("This will be removed by end of summer 2026.")]
        public string? TopicName { get; set; }

        [JsonPropertyName("tenantId")]
        [JsonPropertyOrder(4)]
        public string? TenantId { get; set; }

        [JsonPropertyName("activity")]
        [JsonPropertyOrder(5)]
        public IActivity? Activity { get; set; }

        [JsonPropertyName("channelData")]
        [JsonPropertyOrder(6)]
        public IDictionary<string, object>? ChannelData { get; set; }
    }
}