package de.assecutor.votianlt.messaging.plugin;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * Configuration for messaging plugins.
 * Provides a flexible key-value store for plugin-specific settings.
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PluginConfig {

    /**
     * Plugin-specific properties
     */
    @Builder.Default
    private Map<String, Object> properties = new HashMap<>();

    /**
     * Get a string property.
     *
     * @param key Property key
     * @return Property value or null if not found
     */
    public String getString(String key) {
        Object value = properties.get(key);
        return value != null ? value.toString() : null;
    }

    /**
     * Get a string property with default value.
     *
     * @param key Property key
     * @param defaultValue Default value if property not found
     * @return Property value or default
     */
    public String getString(String key, String defaultValue) {
        String value = getString(key);
        return value != null ? value : defaultValue;
    }

    /**
     * Get an integer property.
     *
     * @param key Property key
     * @return Property value or null if not found
     */
    public Integer getInt(String key) {
        Object value = properties.get(key);
        if (value instanceof Integer) {
            return (Integer) value;
        } else if (value instanceof String) {
            try {
                return Integer.parseInt((String) value);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * Get an integer property with default value.
     *
     * @param key Property key
     * @param defaultValue Default value if property not found
     * @return Property value or default
     */
    public int getInt(String key, int defaultValue) {
        Integer value = getInt(key);
        return value != null ? value : defaultValue;
    }

    /**
     * Get a boolean property.
     *
     * @param key Property key
     * @return Property value or null if not found
     */
    public Boolean getBoolean(String key) {
        Object value = properties.get(key);
        if (value instanceof Boolean) {
            return (Boolean) value;
        } else if (value instanceof String) {
            return Boolean.parseBoolean((String) value);
        }
        return null;
    }

    /**
     * Get a boolean property with default value.
     *
     * @param key Property key
     * @param defaultValue Default value if property not found
     * @return Property value or default
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        Boolean value = getBoolean(key);
        return value != null ? value : defaultValue;
    }

    /**
     * Set a property.
     *
     * @param key Property key
     * @param value Property value
     */
    public void setProperty(String key, Object value) {
        properties.put(key, value);
    }

    /**
     * Check if a property exists.
     *
     * @param key Property key
     * @return true if property exists
     */
    public boolean hasProperty(String key) {
        return properties.containsKey(key);
    }
}

