package dev.lions.unionflow.server.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

/**
 * Entité pour les alertes système.
 * Enregistre les alertes de seuils dépassés, erreurs critiques, etc.
 *
 * @author UnionFlow Team
 * @version 1.0
 * @since 2026-03-15
 */
@Entity
@Table(name = "system_alerts", indexes = {
    @Index(name = "idx_system_alert_timestamp", columnList = "timestamp"),
    @Index(name = "idx_system_alert_level", columnList = "level"),
    @Index(name = "idx_system_alert_acknowledged", columnList = "acknowledged"),
    @Index(name = "idx_system_alert_source", columnList = "source")
})
@Getter
@Setter
public class SystemAlert extends BaseEntity {

    /**
     * Niveau de l'alerte (CRITICAL, ERROR, WARNING, INFO)
     */
    @Column(name = "level", nullable = false, length = 20)
    private String level;

    /**
     * Titre court de l'alerte
     */
    @Column(name = "title", nullable = false, length = 255)
    private String title;

    /**
     * Message détaillé de l'alerte
     */
    @Column(name = "message", nullable = false, length = 1000)
    private String message;

    /**
     * Date/heure de création de l'alerte
     */
    @Column(name = "timestamp", nullable = false)
    private LocalDateTime timestamp;

    /**
     * Alerte acquittée ou non
     */
    @Column(name = "acknowledged", nullable = false)
    private Boolean acknowledged = false;

    /**
     * Email de l'utilisateur ayant acquitté l'alerte
     */
    @Column(name = "acknowledged_by", length = 255)
    private String acknowledgedBy;

    /**
     * Date/heure d'acquittement
     */
    @Column(name = "acknowledged_at")
    private LocalDateTime acknowledgedAt;

    /**
     * Source de l'alerte (CPU, MEMORY, DISK, DATABASE, etc.)
     */
    @Column(name = "source", length = 100)
    private String source;

    /**
     * Type d'alerte (THRESHOLD, INFO, ERROR, etc.)
     */
    @Column(name = "alert_type", length = 50)
    private String alertType;

    /**
     * Valeur actuelle ayant déclenché l'alerte
     */
    @Column(name = "current_value")
    private Double currentValue;

    /**
     * Valeur seuil dépassée
     */
    @Column(name = "threshold_value")
    private Double thresholdValue;

    /**
     * Unité de mesure (%, MB, GB, ms, etc.)
     */
    @Column(name = "unit", length = 20)
    private String unit;

    /**
     * Actions recommandées pour résoudre l'alerte
     */
    @Column(name = "recommended_actions", columnDefinition = "TEXT")
    private String recommendedActions;

    /**
     * Initialisation automatique du timestamp
     */
    @PrePersist
    protected void onCreate() {
        super.onCreate();
        if (timestamp == null) {
            timestamp = LocalDateTime.now();
        }
        if (acknowledged == null) {
            acknowledged = false;
        }
    }
}
