package dev.lions.unionflow.server.api.dto.dashboard;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * DTO principal pour toutes les données du dashboard
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DashboardDataResponse {

    @JsonProperty("stats")
    private DashboardStatsResponse stats;

    @JsonProperty("recentActivities")
    private List<RecentActivityResponse> recentActivities;

    @JsonProperty("upcomingEvents")
    private List<UpcomingEventResponse> upcomingEvents;

    @JsonProperty("userPreferences")
    private Map<String, Object> userPreferences;

    @JsonProperty("organizationId")
    private String organizationId;

    @JsonProperty("userId")
    private String userId;

    // Méthodes utilitaires
    public Integer getTodayEventsCount() {
        if (upcomingEvents == null) {
            return 0;
        }
        
        return (int) upcomingEvents.stream()
                .filter(event -> event.getIsToday() != null && event.getIsToday())
                .count();
    }

    public Integer getTomorrowEventsCount() {
        if (upcomingEvents == null) {
            return 0;
        }
        
        return (int) upcomingEvents.stream()
                .filter(event -> event.getIsTomorrow() != null && event.getIsTomorrow())
                .count();
    }

    public Integer getRecentActivitiesCount() {
        if (recentActivities == null) {
            return 0;
        }
        
        return (int) recentActivities.stream()
                .filter(activity -> activity.getIsRecent() != null && activity.getIsRecent())
                .count();
    }

    public Integer getTodayActivitiesCount() {
        if (recentActivities == null) {
            return 0;
        }
        
        return (int) recentActivities.stream()
                .filter(activity -> activity.getIsToday() != null && activity.getIsToday())
                .count();
    }

    public Boolean getHasUpcomingEvents() {
        return upcomingEvents != null && !upcomingEvents.isEmpty();
    }

    public Boolean getHasRecentActivities() {
        return recentActivities != null && !recentActivities.isEmpty();
    }

    public String getThemePreference() {
        if (userPreferences == null) {
            return "royal_teal";
        }
        return (String) userPreferences.getOrDefault("theme", "royal_teal");
    }

    public String getLanguagePreference() {
        if (userPreferences == null) {
            return "fr";
        }
        return (String) userPreferences.getOrDefault("language", "fr");
    }

    public Boolean getNotificationsEnabled() {
        if (userPreferences == null) {
            return true;
        }
        return (Boolean) userPreferences.getOrDefault("notifications", true);
    }

    public Boolean getAutoRefreshEnabled() {
        if (userPreferences == null) {
            return true;
        }
        return (Boolean) userPreferences.getOrDefault("autoRefresh", true);
    }

    public Integer getRefreshInterval() {
        if (userPreferences == null) {
            return 300; // 5 minutes par défaut
        }
        return (Integer) userPreferences.getOrDefault("refreshInterval", 300);
    }
}
