namespace GmRelay.Bot.Tests.Web;

public sealed class ClubShowcaseSourceTests
{
    [Fact]
    public async Task PublicClubPage_ShouldRenderMembersOnlyBlock()
    {
        var page = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/PublicClub.razor");

        Assert.Contains("Игры для участников клуба", page, StringComparison.Ordinal);
        Assert.Contains("viewerIsActiveMember", page, StringComparison.Ordinal);
        Assert.Contains("members-only-section", page, StringComparison.Ordinal);
    }

    [Fact]
    public async Task PublicClubPage_ShouldRenderApplyAndLoginCtas()
    {
        var page = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/PublicClub.razor");

        Assert.Contains("Подать заявку", page, StringComparison.Ordinal);
        Assert.Contains("Войти как участник", page, StringComparison.Ordinal);
        Assert.Contains("applicationMessage", page, StringComparison.Ordinal);
        Assert.Contains("ApplyForCurrentUserAsync", page, StringComparison.Ordinal);
    }

    [Fact]
    public async Task PublicClubPage_ShouldHideMembersOnlyBlockForAnonymous()
    {
        var page = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/PublicClub.razor");

        // Anonymous users must not see the members-only block content
        Assert.Contains("viewerIsActiveMember", page, StringComparison.Ordinal);
        // Login CTA appears when viewerPlayerId is null
        Assert.Contains("viewerPlayerId is null", page, StringComparison.Ordinal);
    }

    [Fact]
    public async Task PublicLayout_ShouldExposeClubsLink()
    {
        var layout = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Layout/PublicLayout.razor");

        Assert.Contains("href=\"/showcase\"", layout, StringComparison.Ordinal);
        Assert.Contains("Клубы", layout, StringComparison.Ordinal);
    }

    [Fact]
    public async Task NavMenu_ShouldExposeMyClubsLink()
    {
        var menu = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Layout/NavMenu.razor");

        Assert.Contains("href=\"profile/memberships\"", menu, StringComparison.Ordinal);
        Assert.Contains("Мои клубы", menu, StringComparison.Ordinal);
    }

    [Fact]
    public async Task GroupDetails_ShouldExposeApplicationsLink()
    {
        var page = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/GroupDetails.razor");

        Assert.Contains("/applications", page, StringComparison.Ordinal);
        Assert.Contains("Заявки участников", page, StringComparison.Ordinal);
        Assert.Contains("pendingApplicationsCount", page, StringComparison.Ordinal);
    }

    [Fact]
    public async Task GroupDetails_ShouldUsePublicationModeSelectorNotBooleanToggle()
    {
        var page = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/GroupDetails.razor");

        Assert.DoesNotContain("SetSessionPublic(session.Id, !session.IsPublic)", page, StringComparison.Ordinal);
        Assert.DoesNotContain("SetBatchPublic(batch, !batch.AllSessionsPublic)", page, StringComparison.Ordinal);
        Assert.Contains("SetSessionPublicationMode", page, StringComparison.Ordinal);
        Assert.Contains("SetBatchPublicationMode", page, StringComparison.Ordinal);
    }

    [Fact]
    public async Task EditSession_ShouldExposePublicationModeSelector()
    {
        var page = await ReadRepositoryFileAsync("src/GmRelay.Web/Components/Pages/EditSession.razor");

        Assert.Contains("PublicationMode", page, StringComparison.Ordinal);
        Assert.Contains("Режим публикации", page, StringComparison.Ordinal);
        Assert.Contains("Catalog", page, StringComparison.Ordinal);
        Assert.Contains("ClubOnly", page, StringComparison.Ordinal);
        Assert.Contains("Both", page, 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}'.");
    }
}
