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

namespace SharepointToolbox.Tests.Services.Export;

public class StorageHtmlExportServiceTests
{
    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);
    }
    [Fact]
    public void BuildHtml_WithNodes_ContainsToggleJs()
    {
        var svc = new StorageHtmlExportService();
        var nodes = new List<StorageNode>
        {
            new() { Name = "Shared Documents", Library = "Shared Documents", SiteTitle = "Site1",
                    TotalSizeBytes = 5000, FileStreamSizeBytes = 4000, TotalFileCount = 20,
                    Children = new List<StorageNode>
                    {
                        new() { Name = "Archive", Library = "Shared Documents", SiteTitle = "Site1",
                                TotalSizeBytes = 1000, FileStreamSizeBytes = 800, TotalFileCount = 5 }
                    } }
        };
        var html = svc.BuildHtml(nodes);
        Assert.Contains("toggle(", html);
        Assert.Contains("<!DOCTYPE html>", html);
        Assert.Contains("Shared Documents", html);
    }

    [Fact]
    public void BuildHtml_WithEmptyList_ReturnsValidHtml()
    {
        var svc = new StorageHtmlExportService();
        var html = svc.BuildHtml(new List<StorageNode>());
        Assert.Contains("<!DOCTYPE html>", html);
        Assert.Contains("<html", html);
    }

    [Fact]
    public void BuildHtml_WithMultipleLibraries_EachLibraryAppearsInOutput()
    {
        var svc = new StorageHtmlExportService();
        var nodes = new List<StorageNode>
        {
            new() { Name = "Documents", Library = "Documents", SiteTitle = "Site1", TotalSizeBytes = 1000 },
            new() { Name = "Images", Library = "Images", SiteTitle = "Site1", TotalSizeBytes = 2000 }
        };
        var html = svc.BuildHtml(nodes);
        Assert.Contains("Documents", html);
        Assert.Contains("Images", html);
    }

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

    [Fact]
    public void BuildHtml_WithBranding_ContainsLogoImg()
    {
        var svc = new StorageHtmlExportService();
        var nodes = new List<StorageNode>
        {
            new() { Name = "Documents", Library = "Documents", SiteTitle = "Site1", TotalSizeBytes = 1000 }
        };
        var html = svc.BuildHtml(nodes, MakeBranding(msp: true));
        Assert.Contains("data:image/png;base64,bXNw", html);
    }
}
