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.time.temporal.ChronoUnit;

/**
 * DTO pour les activités récentes du dashboard
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RecentActivityResponse {

    @JsonProperty("id")
    private String id;

    @JsonProperty("type")
    private String type;

    @JsonProperty("title")
    private String title;

    @JsonProperty("description")
    private String description;

    @JsonProperty("userName")
    private String userName;

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

    @JsonProperty("userAvatar")
    private String userAvatar;

    @JsonProperty("actionUrl")
    private String actionUrl;

    // Méthodes utilitaires
    public String getTimeAgo() {
        if (timestamp == null) {
            return "";
        }

        LocalDateTime now = LocalDateTime.now();
        long minutes = ChronoUnit.MINUTES.between(timestamp, now);
        long hours = ChronoUnit.HOURS.between(timestamp, now);
        long days = ChronoUnit.DAYS.between(timestamp, now);

        if (minutes < 60) {
            return minutes + "min";
        } else if (hours < 24) {
            return hours + "h";
        } else if (days < 7) {
            return days + "j";
        } else {
            long weeks = days / 7;
            return weeks + "sem";
        }
    }

    public String getActivityIcon() {
        if (type == null) {
            return "help_outline";
        }

        switch (type.toLowerCase()) {
            case "member":
                return "person";
            case "event":
                return "event";
            case "contribution":
                return "payment";
            case "organization":
                return "business";
            case "system":
                return "settings";
            default:
                return "info";
        }
    }

    public String getActivityColor() {
        if (type == null) {
            return "#6B7280"; // grey
        }

        switch (type.toLowerCase()) {
            case "member":
                return "#10B981"; // success
            case "event":
                return "#3B82F6"; // info
            case "contribution":
                return "#008B8B"; // teal blue
            case "organization":
                return "#4169E1"; // royal blue
            case "system":
                return "#6B7280"; // grey
            default:
                return "#6B7280"; // grey
        }
    }

    public Boolean getIsRecent() {
        if (timestamp == null) {
            return false;
        }
        
        LocalDateTime now = LocalDateTime.now();
        long hours = ChronoUnit.HOURS.between(timestamp, now);
        return hours < 24;
    }

    public Boolean getIsToday() {
        if (timestamp == null) {
            return false;
        }
        
        LocalDateTime now = LocalDateTime.now();
        return timestamp.toLocalDate().equals(now.toLocalDate());
    }
}
