using SharepointToolbox.Core.Models;
using SharepointToolbox.Services.Export;

namespace SharepointToolbox.Tests.Services.Export;

/// <summary>
/// Tests for PERM-06: HTML export output.
/// </summary>
public class HtmlExportServiceTests
{
    private static PermissionEntry MakeEntry(
        string users, string userLogins,
        string url = "https://contoso.sharepoint.com/sites/A",
        string principalType = "User") =>
        new("Web", "Site A", url, true, users, userLogins, "Read", "Direct Permissions", principalType);

    private static ReportBranding MakeBranding(bool msp = true, bool client = false)
    {
        var mspLogo    = msp    ? new LogoData { Base64 = "bXNw", MimeType = "image/png" }  : null;
        var clientLogo = client ? new LogoData { Base64 = "Y2xpZW50", MimeType = "image/jpeg" } : null;
        return new ReportBranding(mspLogo, clientLogo);
    }

    private static IReadOnlyDictionary<string, IReadOnlyList<ResolvedMember>> MakeGroupMembers(
        string groupName, params ResolvedMember[] members) =>
        new Dictionary<string, IReadOnlyList<ResolvedMember>>(StringComparer.OrdinalIgnoreCase)
        {
            [groupName] = members.ToList()
        };

    [Fact]
    public void BuildHtml_WithKnownEntries_ContainsUserNames()
    {
        var entry = MakeEntry("Bob Smith", "bob@contoso.com");
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry });

        Assert.Contains("Bob Smith", html);
    }

    [Fact]
    public void BuildHtml_WithEmptyList_ReturnsValidHtml()
    {
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(Array.Empty<PermissionEntry>());

        // Must be non-empty well-formed HTML even with no data rows
        Assert.NotEmpty(html);
        Assert.Contains("<html", html, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void BuildHtml_WithExternalUser_ContainsExtHashMarker()
    {
        // External users have #EXT# in their login — HTML output should make them distinguishable
        var entry = MakeEntry(
            users: "Ext User",
            userLogins: "ext_user_domain.com#EXT#@contoso.onmicrosoft.com");

        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry });

        // The HTML should surface the external marker so admins can identify guests
        Assert.Contains("EXT", html, StringComparison.OrdinalIgnoreCase);
    }

    // ── Branding tests ────────────────────────────────────────────────────────

    [Fact]
    public void BuildHtml_WithMspBranding_ContainsMspLogoImg()
    {
        var entry = MakeEntry("Test", "test@contoso.com");
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry }, MakeBranding(msp: true, client: false));
        Assert.Contains("data:image/png;base64,bXNw", html);
    }

    [Fact]
    public void BuildHtml_WithNullBranding_ContainsNoLogoImg()
    {
        var entry = MakeEntry("Test", "test@contoso.com");
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry });
        Assert.DoesNotContain("data:image/png;base64,", html);
    }

    [Fact]
    public void BuildHtml_WithBothLogos_ContainsTwoImgs()
    {
        var entry = MakeEntry("Test", "test@contoso.com");
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry }, MakeBranding(msp: true, client: true));
        Assert.Contains("data:image/png;base64,bXNw", html);
        Assert.Contains("data:image/jpeg;base64,Y2xpZW50", html);
    }

    // ── Group expansion tests (Phase 17) ──────────────────────────────────────

    [Fact]
    public void BuildHtml_NoGroupMembers_IdenticalToDefault()
    {
        var entry = MakeEntry("Site Members", "i:0#.f|membership|group@contoso.com", principalType: "SharePointGroup");
        var svc = new HtmlExportService();
        var htmlDefault = svc.BuildHtml(new[] { entry });
        var htmlNullNull = svc.BuildHtml(new[] { entry }, null, null);

        Assert.Equal(htmlDefault, htmlNullNull);
    }

    [Fact]
    public void BuildHtml_WithGroupMembers_RendersExpandablePill()
    {
        var entry = MakeEntry("Site Members", "i:0#.f|membership|group@contoso.com", principalType: "SharePointGroup");
        var groupMembers = MakeGroupMembers("Site Members", new ResolvedMember("Alice", "alice@co.com"));
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry }, null, groupMembers);

        Assert.Contains("onclick=\"toggleGroup('grpmem0')\"", html);
        Assert.Contains("class=\"user-pill group-expandable\"", html);
    }

    [Fact]
    public void BuildHtml_WithGroupMembers_RendersHiddenMemberSubRow()
    {
        var entry = MakeEntry("Site Members", "i:0#.f|membership|group@contoso.com", principalType: "SharePointGroup");
        var groupMembers = MakeGroupMembers("Site Members", new ResolvedMember("Alice", "alice@co.com"));
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry }, null, groupMembers);

        Assert.Contains("data-group=\"grpmem0\"", html);
        Assert.Contains("display:none", html);
        Assert.Contains("Alice", html);
        Assert.Contains("alice@co.com", html);
    }

    [Fact]
    public void BuildHtml_WithEmptyMemberList_RendersMembersUnavailable()
    {
        var entry = MakeEntry("Site Members", "i:0#.f|membership|group@contoso.com", principalType: "SharePointGroup");
        var groupMembers = MakeGroupMembers("Site Members"); // empty list
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry }, null, groupMembers);

        Assert.Contains("members unavailable", html);
    }

    [Fact]
    public void BuildHtml_ContainsToggleGroupJs()
    {
        var entry = MakeEntry("Site Members", "i:0#.f|membership|group@contoso.com", principalType: "SharePointGroup");
        var groupMembers = MakeGroupMembers("Site Members", new ResolvedMember("Alice", "alice@co.com"));
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { entry }, null, groupMembers);

        Assert.Contains("function toggleGroup", html);
    }

    [Fact]
    public void BuildHtml_Simplified_WithGroupMembers_RendersExpandablePill()
    {
        var innerEntry = new PermissionEntry("Web", "Site A", "https://contoso.sharepoint.com/sites/A",
            true, "Site Members", "i:0#.f|membership|group@contoso.com", "Read", "Direct Permissions", "SharePointGroup");
        var simplifiedEntry = new SimplifiedPermissionEntry(innerEntry);
        var groupMembers = MakeGroupMembers("Site Members", new ResolvedMember("Bob", "bob@co.com"));
        var svc = new HtmlExportService();
        var html = svc.BuildHtml(new[] { simplifiedEntry }, null, groupMembers);

        Assert.Contains("onclick=\"toggleGroup('grpmem0')\"", html);
        Assert.Contains("class=\"user-pill group-expandable\"", html);
    }
}
