using SharepointToolbox.Services;

namespace SharepointToolbox.Tests.Services;

[Trait("Category", "Unit")]
public class OwnershipElevationServiceTests
{
    [Fact]
    public void OwnershipElevationService_ImplementsIOwnershipElevationService()
    {
        var service = new OwnershipElevationService();
        Assert.IsAssignableFrom<IOwnershipElevationService>(service);
    }

    [Fact]
    public void AppSettings_AutoTakeOwnership_DefaultsFalse()
    {
        var settings = new SharepointToolbox.Core.Models.AppSettings();
        Assert.False(settings.AutoTakeOwnership);
    }

    [Fact]
    public async Task AppSettings_AutoTakeOwnership_RoundTripsThroughJson()
    {
        var json = System.Text.Json.JsonSerializer.Serialize(
            new SharepointToolbox.Core.Models.AppSettings { AutoTakeOwnership = true },
            new System.Text.Json.JsonSerializerOptions { PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase });

        var loaded = System.Text.Json.JsonSerializer.Deserialize<SharepointToolbox.Core.Models.AppSettings>(json,
            new System.Text.Json.JsonSerializerOptions { PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase });

        Assert.NotNull(loaded);
        Assert.True(loaded!.AutoTakeOwnership);
    }

    [Fact]
    public void PermissionEntry_WasAutoElevated_DefaultsFalse()
    {
        var entry = new SharepointToolbox.Core.Models.PermissionEntry(
            "Site", "Title", "https://example.com", false,
            "User", "user@example.com", "Read", "Direct Permissions", "User");

        Assert.False(entry.WasAutoElevated);
    }

    [Fact]
    public void PermissionEntry_WasAutoElevated_TrueWhenSet()
    {
        var entry = new SharepointToolbox.Core.Models.PermissionEntry(
            "Site", "Title", "https://example.com", false,
            "User", "user@example.com", "Read", "Direct Permissions", "User",
            WasAutoElevated: true);

        Assert.True(entry.WasAutoElevated);
    }

    [Fact]
    public void PermissionEntry_WithExpression_CopiesWasAutoElevated()
    {
        var original = new SharepointToolbox.Core.Models.PermissionEntry(
            "Site", "Title", "https://example.com", false,
            "User", "user@example.com", "Read", "Direct Permissions", "User");

        var elevated = original with { WasAutoElevated = true };

        Assert.False(original.WasAutoElevated);
        Assert.True(elevated.WasAutoElevated);
    }
}
