using System; using System.Collections.Generic; using System.Text.Json.Serialization; namespace CredentialManager.Models { /// /// Tipo di mapping field /// public enum MappingType { /// /// Mapping da campo sorgente a campo destinazione /// FieldMapping, /// /// Valore di default per campo destinazione /// DefaultValue } /// /// Rappresenta una voce di mapping che può essere: /// - Un mapping da campo sorgente a campo destinazione /// - Un valore di default per un campo destinazione /// public class FieldMappingEntry { /// /// Tipo di mapping /// [JsonPropertyName("type")] public MappingType Type { get; set; } /// /// Nome del campo sorgente (solo per FieldMapping) /// [JsonPropertyName("sourceField")] public string? SourceField { get; set; } /// /// Nome del campo destinazione /// [JsonPropertyName("destinationField")] public string DestinationField { get; set; } = string.Empty; /// /// Valore di default (solo per DefaultValue) /// [JsonPropertyName("defaultValue")] public object? DefaultValue { get; set; } /// /// Tipo di dato del valore di default (per conversioni corrette) /// Esempi: "string", "int", "decimal", "boolean", "datetime" /// [JsonPropertyName("defaultValueType")] public string? DefaultValueType { get; set; } /// /// Crea un mapping da campo sorgente a campo destinazione /// public static FieldMappingEntry CreateFieldMapping(string sourceField, string destinationField) { return new FieldMappingEntry { Type = MappingType.FieldMapping, SourceField = sourceField, DestinationField = destinationField }; } /// /// Crea un valore di default per un campo destinazione /// public static FieldMappingEntry CreateDefaultValue(string destinationField, object defaultValue, string? valueType = null) { return new FieldMappingEntry { Type = MappingType.DefaultValue, DestinationField = destinationField, DefaultValue = defaultValue, DefaultValueType = valueType ?? InferValueType(defaultValue) }; } /// /// Determina automaticamente il tipo del valore /// private static string InferValueType(object? value) { if (value == null) return "string"; return value switch { string _ => "string", int _ => "int", long _ => "long", decimal _ => "decimal", double _ => "double", float _ => "float", bool _ => "boolean", DateTime _ => "datetime", DateTimeOffset _ => "datetimeoffset", _ => "string" }; } /// /// Ottiene una descrizione user-friendly del mapping /// public string GetDescription() { return Type switch { MappingType.FieldMapping => $"{SourceField} → {DestinationField}", MappingType.DefaultValue => $"{DestinationField} = {DefaultValue ?? "null"} ({DefaultValueType})", _ => "Unknown" }; } } /// /// Helper per la conversione tra vecchio formato (Dictionary) e nuovo formato (FieldMappingEntry) /// public static class MappingConverter { /// /// Converte il vecchio formato Dictionary in lista di FieldMappingEntry /// public static List FromDictionary(Dictionary oldMappings) { var entries = new List(); foreach (var mapping in oldMappings) { entries.Add(FieldMappingEntry.CreateFieldMapping(mapping.Key, mapping.Value)); } return entries; } /// /// Converte una lista di FieldMappingEntry nel vecchio formato Dictionary (solo field mappings) /// public static Dictionary ToDictionary(List entries) { var dictionary = new Dictionary(); foreach (var entry in entries.Where(e => e.Type == MappingType.FieldMapping && !string.IsNullOrEmpty(e.SourceField))) { dictionary[entry.SourceField!] = entry.DestinationField; } return dictionary; } /// /// Ottiene solo i valori di default da una lista di entries /// public static Dictionary GetDefaultValues(List entries) { var defaults = new Dictionary(); foreach (var entry in entries.Where(e => e.Type == MappingType.DefaultValue)) { defaults[entry.DestinationField] = (entry.DefaultValue, entry.DefaultValueType); } return defaults; } } }