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

namespace SharepointToolbox.Tests.Services;

public class BulkResultCsvExportServiceTests
{
    [Fact]
    public void BuildFailedItemsCsv_WithFailedItems_IncludesErrorColumn()
    {
        var service = new BulkResultCsvExportService();
        var items = new List<BulkItemResult<BulkMemberRow>>
        {
            BulkItemResult<BulkMemberRow>.Failed(
                new BulkMemberRow { Email = "bad@test.com", GroupName = "G1", GroupUrl = "http://test", Role = "Member" },
                "User not found"),
        };

        var csv = service.BuildFailedItemsCsv(items);

        Assert.Contains("Error", csv);
        Assert.Contains("Timestamp", csv);
        Assert.Contains("bad@test.com", csv);
        Assert.Contains("User not found", csv);
    }

    [Fact]
    public void BuildFailedItemsCsv_SuccessItems_Excluded()
    {
        var service = new BulkResultCsvExportService();
        var items = new List<BulkItemResult<BulkMemberRow>>
        {
            BulkItemResult<BulkMemberRow>.Success(
                new BulkMemberRow { Email = "ok@test.com", GroupName = "G1", GroupUrl = "http://test", Role = "Member" }),
            BulkItemResult<BulkMemberRow>.Failed(
                new BulkMemberRow { Email = "bad@test.com", GroupName = "G1", GroupUrl = "http://test", Role = "Member" },
                "Error"),
        };

        var csv = service.BuildFailedItemsCsv(items);

        Assert.DoesNotContain("ok@test.com", csv);
        Assert.Contains("bad@test.com", csv);
    }
}
