using System.Text.Json;
using System.Text.Json.Serialization;

namespace Berufsschule_HAM.Models;

public class UserModel
{
    public required string Uid { get; set; }
    public string? Cn { get; set; }
    public string? Sn { get; set; }
    public UserDescription? Description { get; set; }
    public string? JpegPhoto { get; set; }
    public string? Title { get; set; }
    public string? UserPassword { get; set; }
    public UserModel(Dictionary<string, string> ldapData)
    {
        Uid = ldapData.GetValueOrDefault("uid") ?? throw new ArgumentException("UID is required");
        Cn = ldapData.GetValueOrDefault("cn");
        Sn = ldapData.GetValueOrDefault("sn");
        string? description = ldapData.GetValueOrDefault("description");
        Description = description != null ? JsonSerializer.Deserialize<UserDescription>(description) : null;
        JpegPhoto = ldapData.GetValueOrDefault("jpegPhoto");
        Title = ldapData.GetValueOrDefault("title");
        UserPassword = ldapData.GetValueOrDefault("userPassword");
    }
}

public class UserDescription
{
    [JsonPropertyName("BirthDate")]
    public required string BirthDate { get; set; }
    [JsonPropertyName("Address")]
    public required UserAddress Address { get; set; }
    [JsonPropertyName("Workplace")]
    public required string Workplace { get; set; }
    [JsonPropertyName("Groups")]
    public List<string>? Groups { get; set; }
}

public class UserAddress
{
    [JsonPropertyName("City")]
    public string? City { get; set; }
    [JsonPropertyName("Street")]
    public string? Street { get; set; }
    [JsonPropertyName("StreetNr")]
    public int? StreetNr { get; set; }
}

public class UserAuthenticationResult
{
    public required bool Success { get; set; }
    public UserNotAuthenticatedReason AuthenticationState { get; set; } = UserNotAuthenticatedReason.None;
    public UserModel? UserModel { get; set; }
}

public enum UserNotAuthenticatedReason
{
    None,
    InvalidCredentials,
    UserNotAuthorized,
    UserLockedOut
}

public class UserTableViewModel
{
    public required string Uid { get; set; }
    public required string Title { get; set; }
    public required string Name { get; set; }
    public required string Surname { get; set; }
    public required UserDescription Description { get; set; }
}