using SharepointToolbox.Core.Helpers;

namespace SharepointToolbox.Core.Models;

/// <summary>
/// Presentation wrapper around PermissionEntry that adds simplified labels
/// and risk level classification without modifying the immutable source record.
/// Used as the DataGrid ItemsSource when simplified mode is active.
/// </summary>
public class SimplifiedPermissionEntry
{
    /// <summary>The original immutable PermissionEntry.</summary>
    public PermissionEntry Inner { get; }

    /// <summary>
    /// Human-readable labels for the permission levels.
    /// E.g. "Can edit files and list items" instead of "Contribute".
    /// </summary>
    public string SimplifiedLabels { get; }

    /// <summary>
    /// The highest risk level across all permission levels on this entry.
    /// Used for row-level color coding.
    /// </summary>
    public RiskLevel RiskLevel { get; }

    /// <summary>
    /// Individual mapping results for each permission level in the entry.
    /// Used when detailed breakdown per-role is needed.
    /// </summary>
    public IReadOnlyList<PermissionLevelMapping.MappingResult> Mappings { get; }

    // ── Passthrough properties for DataGrid binding ──

    public string ObjectType => Inner.ObjectType;
    public string Title => Inner.Title;
    public string Url => Inner.Url;
    public bool HasUniquePermissions => Inner.HasUniquePermissions;
    public string Users => Inner.Users;
    public string UserLogins => Inner.UserLogins;
    public string PermissionLevels => Inner.PermissionLevels;
    public string GrantedThrough => Inner.GrantedThrough;
    public string PrincipalType => Inner.PrincipalType;
    public string? TargetUrl => Inner.TargetUrl;
    public string? TargetLabel => Inner.TargetLabel;
    public string? SharingLinkType => Inner.SharingLinkType;

    public SimplifiedPermissionEntry(PermissionEntry entry)
    {
        Inner = entry;
        Mappings = PermissionLevelMapping.GetMappings(entry.PermissionLevels);
        SimplifiedLabels = PermissionLevelMapping.GetSimplifiedLabels(entry.PermissionLevels);
        RiskLevel = PermissionLevelMapping.GetHighestRisk(entry.PermissionLevels);
    }

    /// <summary>
    /// Creates SimplifiedPermissionEntry wrappers for a collection of entries.
    /// </summary>
    public static IReadOnlyList<SimplifiedPermissionEntry> WrapAll(
        IEnumerable<PermissionEntry> entries)
    {
        return entries.Select(e => new SimplifiedPermissionEntry(e)).ToList();
    }
}
