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

namespace SharepointToolbox.Tests.Services.Export;

public class SearchExportServiceTests
{
    private static SearchResult MakeSample() => new()
    {
        Title = "Q1 Budget.xlsx",
        Path = "https://contoso.sharepoint.com/sites/Finance/Shared Documents/Q1 Budget.xlsx",
        FileExtension = "xlsx",
        Created = new DateTime(2024, 1, 10),
        LastModified = new DateTime(2024, 3, 20),
        Author = "Alice Smith",
        ModifiedBy = "Bob Jones",
        SizeBytes = 48_000
    };

    // -- CSV tests -----------------------------------------------------------

    [Fact]
    public void BuildCsv_WithKnownResults_ContainsExpectedHeader()
    {
        var svc = new SearchCsvExportService();
        var csv = svc.BuildCsv(new List<SearchResult> { MakeSample() });
        Assert.Contains("File Name", csv);
        Assert.Contains("Extension", csv);
        Assert.Contains("Created", csv);
        Assert.Contains("Created By", csv);
        Assert.Contains("Modified By", csv);
        Assert.Contains("Size", csv);
    }

    [Fact]
    public void BuildCsv_WithEmptyList_ReturnsHeaderOnly()
    {
        var svc = new SearchCsvExportService();
        var csv = svc.BuildCsv(new List<SearchResult>());
        Assert.NotEmpty(csv);
        var lines = csv.Split('\n', StringSplitOptions.RemoveEmptyEntries);
        Assert.Single(lines);
    }

    [Fact]
    public void BuildCsv_ResultValues_AppearInOutput()
    {
        var svc = new SearchCsvExportService();
        var csv = svc.BuildCsv(new List<SearchResult> { MakeSample() });
        Assert.Contains("Alice Smith", csv);
        Assert.Contains("xlsx", csv);
    }

    // -- HTML tests ----------------------------------------------------------

    [Fact]
    public void BuildHtml_WithResults_ContainsSortableColumnScript()
    {
        var svc = new SearchHtmlExportService();
        var html = svc.BuildHtml(new List<SearchResult> { MakeSample() });
        Assert.Contains("<!DOCTYPE html>", html);
        Assert.Contains("sort", html);          // sortable columns JS
        Assert.Contains("Q1 Budget.xlsx", html);
    }

    [Fact]
    public void BuildHtml_WithResults_ContainsFilterInput()
    {
        var svc = new SearchHtmlExportService();
        var html = svc.BuildHtml(new List<SearchResult> { MakeSample() });
        Assert.Contains("filter", html);        // filter input element
    }

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

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

    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_WithBranding_ContainsLogoImg()
    {
        var svc = new SearchHtmlExportService();
        var html = svc.BuildHtml(new List<SearchResult> { MakeSample() }, MakeBranding(msp: true));
        Assert.Contains("data:image/png;base64,bXNw", html);
    }
}
