using SharepointToolbox.Core.Helpers;
using SharepointToolbox.Core.Models;

namespace SharepointToolbox.Tests.Helpers;

/// <summary>
/// Unit tests for PermissionLevelMapping static helper.
/// SIMP-01: Validates mapping correctness for known roles, unknown fallback,
/// case insensitivity, semicolon splitting, risk ranking, and label generation.
/// </summary>
public class PermissionLevelMappingTests
{
    [Theory]
    [InlineData("Full Control", RiskLevel.High)]
    [InlineData("Site Collection Administrator", RiskLevel.High)]
    [InlineData("Contribute", RiskLevel.Medium)]
    [InlineData("Edit", RiskLevel.Medium)]
    [InlineData("Design", RiskLevel.Medium)]
    [InlineData("Approve", RiskLevel.Medium)]
    [InlineData("Manage Hierarchy", RiskLevel.Medium)]
    [InlineData("Read", RiskLevel.Low)]
    [InlineData("Restricted Read", RiskLevel.Low)]
    [InlineData("View Only", RiskLevel.ReadOnly)]
    [InlineData("Restricted View", RiskLevel.ReadOnly)]
    public void GetMapping_KnownRoles_ReturnsCorrectRiskLevel(string roleName, RiskLevel expected)
    {
        var result = PermissionLevelMapping.GetMapping(roleName);
        Assert.Equal(expected, result.RiskLevel);
        Assert.NotEmpty(result.Label);
    }

    [Fact]
    public void GetMapping_UnknownRole_ReturnsMediumRiskWithRawName()
    {
        var result = PermissionLevelMapping.GetMapping("Custom Permission Level");
        Assert.Equal(RiskLevel.Medium, result.RiskLevel);
        Assert.Equal("Custom Permission Level", result.Label);
    }

    [Fact]
    public void GetMapping_CaseInsensitive()
    {
        var lower = PermissionLevelMapping.GetMapping("full control");
        var upper = PermissionLevelMapping.GetMapping("FULL CONTROL");
        Assert.Equal(RiskLevel.High, lower.RiskLevel);
        Assert.Equal(RiskLevel.High, upper.RiskLevel);
    }

    [Fact]
    public void GetMappings_SemicolonDelimited_SplitsAndMaps()
    {
        var results = PermissionLevelMapping.GetMappings("Full Control; Read");
        Assert.Equal(2, results.Count);
        Assert.Equal(RiskLevel.High, results[0].RiskLevel);
        Assert.Equal(RiskLevel.Low, results[1].RiskLevel);
    }

    [Fact]
    public void GetMappings_EmptyString_ReturnsEmpty()
    {
        var results = PermissionLevelMapping.GetMappings("");
        Assert.Empty(results);
    }

    [Fact]
    public void GetHighestRisk_MultipleLevels_ReturnsHighest()
    {
        // Full Control (High) + Read (Low) => High
        var risk = PermissionLevelMapping.GetHighestRisk("Full Control; Read");
        Assert.Equal(RiskLevel.High, risk);
    }

    [Fact]
    public void GetHighestRisk_SingleReadOnly_ReturnsReadOnly()
    {
        var risk = PermissionLevelMapping.GetHighestRisk("View Only");
        Assert.Equal(RiskLevel.ReadOnly, risk);
    }

    [Fact]
    public void GetSimplifiedLabels_JoinsLabels()
    {
        var labels = PermissionLevelMapping.GetSimplifiedLabels("Contribute; Read");
        Assert.Contains("Can edit files and list items", labels);
        Assert.Contains("Can view files and pages", labels);
    }
}
