package dev.lions.unionflow.server.entity;

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

import java.time.LocalDateTime;

/**
 * Entité pour les logs techniques du système.
 * Enregistre les erreurs, warnings, et événements système.
 *
 * @author UnionFlow Team
 * @version 1.0
 * @since 2026-03-15
 */
@Entity
@Table(name = "system_logs", indexes = {
    @Index(name = "idx_system_log_timestamp", columnList = "timestamp"),
    @Index(name = "idx_system_log_level", columnList = "level"),
    @Index(name = "idx_system_log_source", columnList = "source"),
    @Index(name = "idx_system_log_user_id", columnList = "user_id")
})
@Getter
@Setter
public class SystemLog extends BaseEntity {

    /**
     * Niveau du log (CRITICAL, ERROR, WARNING, INFO, DEBUG)
     */
    @Column(name = "level", nullable = false, length = 20)
    private String level;

    /**
     * Source du log (Database, API, Auth, System, Cache, etc.)
     */
    @Column(name = "source", nullable = false, length = 100)
    private String source;

    /**
     * Message principal du log
     */
    @Column(name = "message", nullable = false, length = 1000)
    private String message;

    /**
     * Détails supplémentaires (stacktrace, contexte, etc.)
     */
    @Column(name = "details", columnDefinition = "TEXT")
    private String details;

    /**
     * Date/heure du log
     */
    @Column(name = "timestamp", nullable = false)
    private LocalDateTime timestamp;

    /**
     * Identifiant de l'utilisateur concerné (optionnel)
     */
    @Column(name = "user_id", length = 255)
    private String userId;

    /**
     * Adresse IP de la requête (optionnel)
     */
    @Column(name = "ip_address", length = 45)
    private String ipAddress;

    /**
     * Identifiant de session (optionnel)
     */
    @Column(name = "session_id", length = 255)
    private String sessionId;

    /**
     * Endpoint HTTP concerné (optionnel)
     */
    @Column(name = "endpoint", length = 500)
    private String endpoint;

    /**
     * Code de statut HTTP (optionnel)
     */
    @Column(name = "http_status_code")
    private Integer httpStatusCode;

    /**
     * Initialisation automatique du timestamp
     */
    @PrePersist
    protected void onCreate() {
        super.onCreate(); // Appel du @PrePersist de BaseEntity (dateCreation, actif)
        if (timestamp == null) {
            timestamp = LocalDateTime.now();
        }
    }
}
