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

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

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * DTO pour les statistiques du dashboard
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DashboardStatsResponse {

    @JsonProperty("totalMembers")
    private Integer totalMembers;

    @JsonProperty("activeMembers")
    private Integer activeMembers;

    @JsonProperty("totalEvents")
    private Integer totalEvents;

    @JsonProperty("upcomingEvents")
    private Integer upcomingEvents;

    @JsonProperty("totalContributions")
    private Integer totalContributions;

    @JsonProperty("totalContributionAmount")
    private Double totalContributionAmount;

    @JsonProperty("pendingRequests")
    private Integer pendingRequests;

    @JsonProperty("completedProjects")
    private Integer completedProjects;

    @JsonProperty("monthlyGrowth")
    private Double monthlyGrowth;

    @JsonProperty("engagementRate")
    private Double engagementRate;

    @JsonProperty("lastUpdated")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime lastUpdated;

    /**
     * Nombre total d'organisations dans le système (SuperAdmin uniquement)
     */
    @JsonProperty("totalOrganizations")
    private Integer totalOrganizations;

    /**
     * Répartition des organisations par type
     * Exemple: {"Mutuelle": 15, "Coopérative": 8, "Tontine": 5, "Autre": 3}
     */
    @JsonProperty("organizationTypeDistribution")
    private Map<String, Integer> organizationTypeDistribution;

    /**
     * Données historiques mensuelles pour les graphiques (12 derniers mois)
     */
    @JsonProperty("monthlyHistoricalData")
    private List<MonthlyStatDTO> monthlyHistoricalData;

    // Méthodes utilitaires
    public String getFormattedContributionAmount() {
        if (totalContributionAmount == null) {
            return "0";
        }
        
        if (totalContributionAmount >= 1_000_000) {
            return String.format("%.1fM", totalContributionAmount / 1_000_000);
        } else if (totalContributionAmount >= 1_000) {
            return String.format("%.0fK", totalContributionAmount / 1_000);
        } else {
            return String.format("%.0f", totalContributionAmount);
        }
    }

    public Boolean getHasGrowth() {
        return monthlyGrowth != null && monthlyGrowth > 0;
    }

    public Boolean getIsHighEngagement() {
        return engagementRate != null && engagementRate > 0.7;
    }

    public Double getInactiveMembers() {
        if (totalMembers == null || activeMembers == null) {
            return 0.0;
        }
        return (double) (totalMembers - activeMembers);
    }

    public Double getActiveMemberPercentage() {
        if (totalMembers == null || activeMembers == null || totalMembers == 0) {
            return 0.0;
        }
        return (double) activeMembers / totalMembers * 100;
    }

    public Double getEngagementPercentage() {
        if (engagementRate == null) {
            return 0.0;
        }
        return engagementRate * 100;
    }
}
