using System.Text.Json;
using System.Text.Json.Serialization;
using Berufsschule_HAM.Exceptions;

namespace Berufsschule_HAM.Models;

public class GroupModel
{
    public required string Cn { get; set; }
    public string? GidNumber { get; set; }
    public List<GroupPermission> Permissions { get; set; }
    public GroupModel(Dictionary<string, string> ldapData)
    {
        Cn = ldapData.GetValueOrDefault("cn") ?? throw new GroupModelConfigurationException();
        GidNumber = ldapData.GetValueOrDefault("gidNumber");
        string? descriptionValue = ldapData.GetValueOrDefault("description");
        if (descriptionValue is null)
        {
            Permissions = [];
        }
        else
        {
            Permissions = JsonSerializer.Deserialize<GroupPermissions>(descriptionValue)?.Permissions ?? [];
        }
    }
}

public class GroupPermissions
{
    public required List<GroupPermission> Permissions { get; set; }
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum GroupPermission
{
    None,
    CanInventorize,
    CanManageUsers,
    CanManageLocations,
    CanManageAssets,
    CanManageGroups
}