package com.social.media.domain.user.valueobject;

import java.util.Map;

/**
 * Value Object representing user configuration settings
 */
public record UserConfiguration(
    String timezone,
    String language,
    Map<String, Object> preferences
) {
    
    public UserConfiguration {
        if (timezone == null || timezone.trim().isEmpty()) {
            timezone = "America/Sao_Paulo";
        }
        if (language == null || language.trim().isEmpty()) {
            language = "pt-BR";
        }
        preferences = preferences != null ? Map.copyOf(preferences) : Map.of();
    }
    
    public static UserConfiguration defaultConfig() {
        return new UserConfiguration("America/Sao_Paulo", "pt-BR", Map.of());
    }
    
    public Object getPreference(String key) {
        return preferences.get(key);
    }
    
    public <T> T getPreference(String key, Class<T> type) {
        Object value = preferences.get(key);
        if (value != null && type.isInstance(value)) {
            return type.cast(value);
        }
        return null;
    }
    
    public UserConfiguration withPreference(String key, Object value) {
        Map<String, Object> newPreferences = Map.copyOf(preferences);
        newPreferences.put(key, value);
        return new UserConfiguration(timezone, language, newPreferences);
    }
    
    public UserConfiguration withTimezone(String newTimezone) {
        return new UserConfiguration(newTimezone, language, preferences);
    }
    
    public UserConfiguration withLanguage(String newLanguage) {
        return new UserConfiguration(timezone, newLanguage, preferences);
    }
    
    public boolean hasPreference(String key) {
        return preferences.containsKey(key);
    }
}
