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

namespace SharepointToolbox.Tests.Services.Export;

public class DuplicatesHtmlExportServiceTests
{
    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 DuplicateGroup MakeGroup(string name, int count) => new()
    {
        GroupKey = $"{name}|1024",
        Name = name,
        Items = Enumerable.Range(1, count).Select(i => new DuplicateItem
        {
            Name = name,
            Path = $"https://contoso.sharepoint.com/sites/Site{i}/{name}",
            Library = "Shared Documents",
            SizeBytes = 1024
        }).ToList()
    };

    [Fact]
    public void BuildHtml_WithGroups_ContainsGroupCards()
    {
        var svc = new DuplicatesHtmlExportService();
        var groups = new List<DuplicateGroup> { MakeGroup("report.docx", 3) };
        var html = svc.BuildHtml(groups);
        Assert.Contains("<!DOCTYPE html>", html);
        Assert.Contains("report.docx", html);
    }

    [Fact]
    public void BuildHtml_WithMultipleGroups_AllGroupNamesPresent()
    {
        var svc = new DuplicatesHtmlExportService();
        var groups = new List<DuplicateGroup>
        {
            MakeGroup("budget.xlsx", 2),
            MakeGroup("photo.jpg", 4)
        };
        var html = svc.BuildHtml(groups);
        Assert.Contains("budget.xlsx", html);
        Assert.Contains("photo.jpg", html);
    }

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

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

    [Fact]
    public void BuildHtml_WithBranding_ContainsLogoImg()
    {
        var svc = new DuplicatesHtmlExportService();
        var groups = new List<DuplicateGroup> { MakeGroup("report.docx", 2) };
        var html = svc.BuildHtml(groups, MakeBranding(msp: true));
        Assert.Contains("data:image/png;base64,bXNw", html);
    }
}
