namespace GmRelay.Bot.Tests.Web;

public sealed class GroupDetailsSourceTests
{
    [Fact]
    public async Task GroupDetails_ShouldManageCoGmUsingGroupPlatform()
    {
        var source = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/GroupDetails.razor");

        Assert.Contains("CoGmPlatform", source, StringComparison.Ordinal);
        Assert.Contains("@CoGmIdLabel", source, StringComparison.Ordinal);
        Assert.Contains("coGmModel.ExternalUserId", source, StringComparison.Ordinal);
        Assert.Matches(@"SessionService\.AddCoGmForOwnerAsync\(\s*GroupId,\s*CoGmPlatform,\s*coGmExternalUserId", source);
        Assert.Matches(@"SessionService\.RemoveCoGmForOwnerAsync\(GroupId,\s*platform,\s*coGmExternalUserId\)", source);
        Assert.DoesNotContain("Telegram ID co-GM", source, StringComparison.Ordinal);
        Assert.DoesNotContain("SessionService.RemoveCoGmForOwnerAsync(GroupId, \"Telegram\"", source, StringComparison.Ordinal);
    }

    private static async Task<string> ReadRepositoryFileAsync(string relativePath)
    {
        var directory = new DirectoryInfo(AppContext.BaseDirectory);
        while (directory is not null)
        {
            var candidate = Path.Combine(directory.FullName, relativePath);
            if (File.Exists(candidate))
            {
                return await File.ReadAllTextAsync(candidate);
            }

            directory = directory.Parent;
        }

        throw new FileNotFoundException($"Could not locate repository file '{relativePath}'.");
    }
}
