package com.social.media.domain.socialaccount.valueobject;

import java.util.Map;

/**
 * Value Object representing automation configuration for social accounts
 */
public record AutomationConfiguration(
    boolean autoLikeEnabled,
    boolean autoFollowEnabled,
    boolean autoCommentEnabled,
    boolean autoDirectMessageEnabled,
    int dailyActionLimit,
    int hourlyActionLimit,
    Map<String, Object> customSettings
) {
    
    public AutomationConfiguration {
        if (dailyActionLimit < 0) {
            throw new IllegalArgumentException("Daily action limit cannot be negative");
        }
        if (hourlyActionLimit < 0) {
            throw new IllegalArgumentException("Hourly action limit cannot be negative");
        }
        if (hourlyActionLimit > dailyActionLimit && dailyActionLimit > 0) {
            throw new IllegalArgumentException("Hourly limit cannot exceed daily limit");
        }
        customSettings = customSettings != null ? Map.copyOf(customSettings) : Map.of();
    }
    
    public static AutomationConfiguration disabled() {
        return new AutomationConfiguration(false, false, false, false, 0, 0, Map.of());
    }
    
    public static AutomationConfiguration conservative() {
        return new AutomationConfiguration(true, false, false, false, 50, 10, Map.of());
    }
    
    public static AutomationConfiguration moderate() {
        return new AutomationConfiguration(true, true, false, false, 100, 20, Map.of());
    }
    
    public static AutomationConfiguration aggressive() {
        return new AutomationConfiguration(true, true, true, false, 200, 40, Map.of());
    }
    
    public boolean hasAnyAutomationEnabled() {
        return autoLikeEnabled || autoFollowEnabled || autoCommentEnabled || autoDirectMessageEnabled;
    }
    
    public boolean hasActionLimits() {
        return dailyActionLimit > 0 || hourlyActionLimit > 0;
    }
    
    public Object getCustomSetting(String key) {
        return customSettings.get(key);
    }
    
    public <T> T getCustomSetting(String key, Class<T> type) {
        Object value = customSettings.get(key);
        if (value != null && type.isInstance(value)) {
            return type.cast(value);
        }
        return null;
    }
    
    public AutomationConfiguration withCustomSetting(String key, Object value) {
        Map<String, Object> newSettings = Map.copyOf(customSettings);
        newSettings.put(key, value);
        return new AutomationConfiguration(
            autoLikeEnabled, autoFollowEnabled, autoCommentEnabled, autoDirectMessageEnabled,
            dailyActionLimit, hourlyActionLimit, newSettings
        );
    }
}
