using System.Security.Claims;
using GmRelay.Web.Services;

namespace GmRelay.Bot.Tests.Web;

public class PlatformIdentityTests
{
    [Fact]
    public void TryGetPlatformIdentity_TelegramUser_ReturnsTelegramPlatform()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.NameIdentifier, "12345"),
            new Claim("TelegramId", "12345")
        }));

        var result = user.TryGetPlatformIdentity(out var platform, out var externalUserId);

        Assert.True(result);
        Assert.Equal("Telegram", platform);
        Assert.Equal("12345", externalUserId);
    }

    [Fact]
    public void TryGetPlatformIdentity_TelegramUserWithPlatformClaim_ReturnsTelegramPlatform()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.NameIdentifier, "12345"),
            new Claim("Platform", "Telegram")
        }));

        var result = user.TryGetPlatformIdentity(out var platform, out var externalUserId);

        Assert.True(result);
        Assert.Equal("Telegram", platform);
        Assert.Equal("12345", externalUserId);
    }

    [Fact]
    public void TryGetPlatformIdentity_DiscordUser_ReturnsDiscordPlatform()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.NameIdentifier, "98765"),
            new Claim("DiscordId", "98765"),
            new Claim("Platform", "Discord")
        }));

        var result = user.TryGetPlatformIdentity(out var platform, out var externalUserId);

        Assert.True(result);
        Assert.Equal("Discord", platform);
        Assert.Equal("98765", externalUserId);
    }

    [Fact]
    public void TryGetPlatformIdentity_UnknownUser_ReturnsFalse()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.Name, "Anonymous")
        }));

        var result = user.TryGetPlatformIdentity(out _, out _);
        Assert.False(result);
    }

    [Fact]
    public void TryGetDiscordId_DiscordUser_ReturnsTrue()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim("DiscordId", "123")
        }));

        var result = user.TryGetDiscordId(out var discordId);
        Assert.True(result);
        Assert.Equal("123", discordId);
    }

    [Fact]
    public void TryGetDiscordId_TelegramUser_ReturnsFalse()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim("TelegramId", "123")
        }));

        var result = user.TryGetDiscordId(out _);
        Assert.False(result);
    }

    [Fact]
    public void GetAvatarUrl_DiscordUserWithAvatar_ReturnsUrl()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
        {
            new Claim("AvatarUrl", "https://cdn.discordapp.com/avatars/1/abc.png")
        }));

        var result = user.GetAvatarUrl();
        Assert.Equal("https://cdn.discordapp.com/avatars/1/abc.png", result);
    }

    [Fact]
    public void GetAvatarUrl_UserWithoutAvatar_ReturnsNull()
    {
        var user = new ClaimsPrincipal(new ClaimsIdentity(Array.Empty<Claim>()));

        var result = user.GetAvatarUrl();
        Assert.Null(result);
    }
}
