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

namespace Berufsschule_HAM.Models;

public class GroupModel
{
    [JsonPropertyName("Cn")]
    public required string Cn { get; set; }
    [JsonPropertyName("DisplayName")]
    public string DisplayName { get; set; }
    [JsonPropertyName("GidNumber")]
    public string? GidNumber { get; set; }
    [JsonPropertyName("Permissions")]
    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)
        {
            DisplayName = Cn;
            Permissions = [];
        }
        else
        {
            GroupDescription? description = JsonSerializer.Deserialize<GroupDescription>(descriptionValue);
            DisplayName = description?.DisplayName ?? Cn;
            Permissions = description?.Permissions ?? [];
        }
    }
}

public class GroupDescription
{
    [JsonPropertyName("DisplayName")]
    public required string DisplayName { get; set; }
    [JsonPropertyName("Permissions")]
    public required List<GroupPermission> Permissions { get; set; }
}

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