package dev.lions.unionflow.server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * Entité Notification pour la gestion des notifications
 *
 * @author UnionFlow Team
 * @version 3.0
 * @since 2025-01-29
 */
@Entity
@Table(name = "notifications", indexes = {
    @Index(name = "idx_notification_type", columnList = "type_notification"),
    @Index(name = "idx_notification_statut", columnList = "statut"),
    @Index(name = "idx_notification_priorite", columnList = "priorite"),
    @Index(name = "idx_notification_membre", columnList = "membre_id"),
    @Index(name = "idx_notification_organisation", columnList = "organisation_id"),
    @Index(name = "idx_notification_date_envoi", columnList = "date_envoi")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class Notification extends BaseEntity {

  /** Type de notification */
  @NotNull
  @Column(name = "type_notification", nullable = false, length = 30)
  private String typeNotification;

  /** Priorité */
  @Builder.Default
  @Column(name = "priorite", length = 20)
  private String priorite = "NORMALE";

  /** Statut */
  @Builder.Default
  @Column(name = "statut", length = 30)
  private String statut = "EN_ATTENTE";

  /** Sujet */
  @Column(name = "sujet", length = 500)
  private String sujet;

  /** Corps du message */
  @Column(name = "corps", columnDefinition = "TEXT")
  private String corps;

  /** Date d'envoi prévue */
  @Column(name = "date_envoi_prevue")
  private LocalDateTime dateEnvoiPrevue;

  /** Date d'envoi réelle */
  @Column(name = "date_envoi")
  private LocalDateTime dateEnvoi;

  /** Date de lecture */
  @Column(name = "date_lecture")
  private LocalDateTime dateLecture;

  /** Nombre de tentatives d'envoi */
  @Builder.Default
  @Column(name = "nombre_tentatives", nullable = false)
  private Integer nombreTentatives = 0;

  /** Message d'erreur (si échec) */
  @Column(name = "message_erreur", length = 1000)
  private String messageErreur;

  /** Données additionnelles (JSON) */
  @Column(name = "donnees_additionnelles", columnDefinition = "TEXT")
  private String donneesAdditionnelles;

  // Relations
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "membre_id")
  private Membre membre;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "organisation_id")
  private Organisation organisation;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "template_id")
  private TemplateNotification template;

  /** Méthode métier pour vérifier si la notification est envoyée */
  public boolean isEnvoyee() {
    return statut != null && dev.lions.unionflow.server.api.enums.notification.StatutNotification.ENVOYEE.name().equals(statut);
  }

  /** Méthode métier pour vérifier si la notification est lue */
  public boolean isLue() {
    return statut != null && dev.lions.unionflow.server.api.enums.notification.StatutNotification.LUE.name().equals(statut);
  }

  /** Callback JPA avant la persistance */
  @PrePersist
  protected void onCreate() {
    super.onCreate();
    if (priorite == null) {
      priorite = "NORMALE";
    }
    if (statut == null) {
      statut = "EN_ATTENTE";
    }
    if (nombreTentatives == null) {
      nombreTentatives = 0;
    }
    if (dateEnvoiPrevue == null) {
      dateEnvoiPrevue = LocalDateTime.now();
    }
  }
}
