using System.Net;

using Microsoft.Teams.Api.Clients;
using Microsoft.Teams.Api.Meetings;
using Microsoft.Teams.Common.Http;

using Moq;

namespace Microsoft.Teams.Api.Tests.Clients;

public class MeetingClientTests
{
    [Fact]
    public async Task MeetingClient_GetByIdAsync()
    {
        var responseMessage = new HttpResponseMessage();
        responseMessage.Headers.Add("Custom-Header", "HeaderValue");
        var mockHandler = new Mock<IHttpClient>();
        mockHandler
            .Setup(handler => handler.SendAsync<Meeting>(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync(new HttpResponse<Meeting>()
            {
                Headers = responseMessage.Headers,
                StatusCode = HttpStatusCode.OK,
                Body = new Meeting { Id = "meeting123" }
            });

        string serviceUrl = "https://serviceurl.com/";
        string meetingId = "meeting123";
        var meetingClient = new MeetingClient(serviceUrl, mockHandler.Object);

        var result = await meetingClient.GetByIdAsync(meetingId);

        Assert.Equal("meeting123", result.Id);

        string expectedUrl = "https://serviceurl.com/v1/meetings/meeting123";
        HttpMethod expectedMethod = HttpMethod.Get;
        mockHandler.Verify(x => x.SendAsync<Meeting>(
            It.Is<IHttpRequest>(arg => arg.Url == expectedUrl && arg.Method == expectedMethod),
            It.IsAny<CancellationToken>()),
            Times.Once);
    }

    [Fact]
    public async Task MeetingClient_GetParticipantAsync()
    {
        var responseMessage = new HttpResponseMessage();
        responseMessage.Headers.Add("Custom-Header", "HeaderValue");
        var mockHandler = new Mock<IHttpClient>();
        mockHandler
            .Setup(handler => handler.SendAsync<MeetingParticipant>(It.IsAny<IHttpRequest>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync(new HttpResponse<MeetingParticipant>()
            {
                Headers = responseMessage.Headers,
                StatusCode = HttpStatusCode.OK,
                Body = new MeetingParticipant
                {
                    User = new Account { Id = "user1", Name = "John Doe" },
                    Meeting = new MeetingInfo { Role = "Presenter", InMeeting = true },
                    Conversation = new Conversation { Id = "conversation1", Name = "General" }
                }
            });

        string serviceUrl = "https://serviceurl.com/";
        string meetingId = "meeting123";
        string participantId = "participant1";
        var meetingClient = new MeetingClient(serviceUrl, mockHandler.Object);

        var result = await meetingClient.GetParticipantAsync(meetingId, participantId, "tenant-id");

        Assert.Equal("user1", result.User?.Id);
        Assert.Equal("John Doe", result.User?.Name);
        Assert.Equal("Presenter", result.Meeting?.Role);
        Assert.True(result.Meeting?.InMeeting);

        string expectedUrl = "https://serviceurl.com/v1/meetings/meeting123/participants/participant1?tenantId=tenant-id";
        HttpMethod expectedMethod = HttpMethod.Get;
        mockHandler.Verify(x => x.SendAsync<MeetingParticipant>(
            It.Is<IHttpRequest>(arg => arg.Url == expectedUrl && arg.Method == expectedMethod),
            It.IsAny<CancellationToken>()),
            Times.Once);
    }
}