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

namespace SharepointToolbox.Tests.Services.Export;

public class StorageCsvExportServiceTests
{
    [Fact]
    public void BuildCsv_WithKnownNodes_ProducesHeaderRow()
    {
        var svc = new StorageCsvExportService();
        var nodes = new List<StorageNode>
        {
            new() { Name = "Shared Documents", Library = "Shared Documents", SiteTitle = "MySite",
                    TotalSizeBytes = 1024, FileStreamSizeBytes = 800, TotalFileCount = 5,
                    LastModified = new DateTime(2024, 1, 15) }
        };
        var csv = svc.BuildCsv(nodes);
        Assert.Contains("Library", csv);
        Assert.Contains("Site", csv);
        Assert.Contains("Files", csv);
        Assert.Contains("Total Size", csv);
        Assert.Contains("Version Size", csv);
        Assert.Contains("Last Modified", csv);
    }

    [Fact]
    public void BuildCsv_WithEmptyList_ReturnsHeaderOnly()
    {
        var svc = new StorageCsvExportService();
        var csv = svc.BuildCsv(new List<StorageNode>());
        Assert.NotEmpty(csv); // must have at least the header row
        var lines = csv.Split('\n', StringSplitOptions.RemoveEmptyEntries);
        Assert.Single(lines); // only header, no data rows
    }

    [Fact]
    public void BuildCsv_NodeValues_AppearInOutput()
    {
        var svc = new StorageCsvExportService();
        var nodes = new List<StorageNode>
        {
            new() { Name = "Reports", Library = "Reports", SiteTitle = "ProjectSite",
                    TotalSizeBytes = 2048, FileStreamSizeBytes = 1024, TotalFileCount = 10 }
        };
        var csv = svc.BuildCsv(nodes);
        Assert.Contains("Reports", csv);
        Assert.Contains("ProjectSite", csv);
        Assert.Contains("10", csv);
    }
}
