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

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

namespace Microsoft.Teams.Apps.UnitTests;

/// <summary>
/// Tests for simple activity types.
/// </summary>
public class ActivitiesTests
{
    [Fact]
    public void MessageReaction_FromActivityConvertsCorrectly()
    {
        CoreActivity coreActivity = new()
        {
            Type = TeamsActivityType.MessageReaction
        };
        coreActivity.Properties["reactionsAdded"] = System.Text.Json.JsonSerializer.SerializeToElement(new[]
        {
            new { type = "like" },
            new { type = "heart" }
        });

        MessageReactionActivity activity = MessageReactionActivity.FromActivity(coreActivity);
        Assert.NotNull(activity);
        Assert.Equal(TeamsActivityType.MessageReaction, activity.Type);
        Assert.NotNull(activity.ReactionsAdded);
        Assert.Equal(2, activity.ReactionsAdded!.Count);
    }

    [Fact]
    public void MessageDelete_Constructor_Default_SetsMessageDeleteType()
    {
        MessageDeleteActivity activity = new();
        Assert.Equal(TeamsActivityType.MessageDelete, activity.Type);
    }

    [Fact]
    public void MessageDelete_FromActivityConvertsCorrectly()
    {
        CoreActivity coreActivity = new()
        {
            Type = TeamsActivityType.MessageDelete,
            Id = "deleted-msg-id"
        };

        MessageDeleteActivity messageDelete = MessageDeleteActivity.FromActivity(coreActivity);
        Assert.NotNull(messageDelete);
        Assert.Equal(TeamsActivityType.MessageDelete, messageDelete.Type);
        Assert.Equal("deleted-msg-id", messageDelete.Id);
    }

    [Fact]
    public void MessageUpdate_Constructor_Default_SetsMessageUpdateType()
    {
        MessageUpdateActivity activity = new();
        Assert.Equal(TeamsActivityType.MessageUpdate, activity.Type);
    }

    [Fact]
    public void MessageUpdate_Constructor_WithText_SetsTextAndMessageUpdateType()
    {
        MessageUpdateActivity activity = new("Updated text");
        Assert.Equal(TeamsActivityType.MessageUpdate, activity.Type);
        Assert.Equal("Updated text", activity.Text);
    }

    [Fact]
    public void MessageUpdate_InheritsFromMessageActivity()
    {
        MessageUpdateActivity activity = new()
        {
            Text = "Updated",
            TextFormat = TextFormats.Markdown
        };

        Assert.Equal("Updated", activity.Text);
        //Assert.Equal(InputHints.AcceptingInput, activity.InputHint);
        Assert.Equal(TextFormats.Markdown, activity.TextFormat);
    }

    [Fact]
    public void MessageUpdate_FromActivityConvertsCorrectly()
    {
        CoreActivity coreActivity = new()
        {
            Type = TeamsActivityType.MessageUpdate
        };
        coreActivity.Properties["text"] = "Test message";

        MessageUpdateActivity messageUpdate = MessageUpdateActivity.FromActivity(coreActivity);
        Assert.NotNull(messageUpdate);
        Assert.Equal(TeamsActivityType.MessageUpdate, messageUpdate.Type);
        Assert.Equal("Test message", messageUpdate.Text);
    }

    [Fact]
    public void ConversationUpdate_Constructor_Default_SetsConversationUpdateType()
    {
        ConversationUpdateActivity activity = new();
        Assert.Equal(TeamsActivityType.ConversationUpdate, activity.Type);
    }

    [Fact]
    public void ConversationUpdate_FromActivityConvertsCorrectly()
    {
        CoreActivity coreActivity = new()
        {
            Type = TeamsActivityType.ConversationUpdate
        };
        //coreActivity.Properties["topicName"] = "Converted Topic";

        ConversationUpdateActivity activity = ConversationUpdateActivity.FromActivity(coreActivity);
        Assert.NotNull(activity);
        Assert.Equal(TeamsActivityType.ConversationUpdate, activity.Type);
        //Assert.Equal("Converted Topic", activity.TopicName);
    }

    [Fact]
    public void InstallUpdate_Constructor_Default_SetsInstallationUpdateType()
    {
        InstallUpdateActivity activity = new();
        Assert.Equal(TeamsActivityType.InstallationUpdate, activity.Type);
    }

    [Fact]
    public void InstallUpdate_FromActivityConvertsCorrectly()
    {
        CoreActivity coreActivity = new()
        {
            Type = TeamsActivityType.InstallationUpdate
        };
        coreActivity.Properties["action"] = "remove";

        InstallUpdateActivity activity = InstallUpdateActivity.FromActivity(coreActivity);
        Assert.NotNull(activity);
        Assert.Equal(TeamsActivityType.InstallationUpdate, activity.Type);
        Assert.Equal("remove", activity.Action);
    }
}
