package dev.lions.unionflow.server.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * Entité TemplateNotification pour les templates de notifications réutilisables
 *
 * @author UnionFlow Team
 * @version 3.0
 * @since 2025-01-29
 */
@Entity
@Table(
    name = "templates_notifications",
    indexes = {
      @Index(name = "idx_template_code", columnList = "code", unique = true),
      @Index(name = "idx_template_actif", columnList = "actif")
    })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class TemplateNotification extends BaseEntity {

  /** Code unique du template */
  @NotBlank
  @Column(name = "code", unique = true, nullable = false, length = 100)
  private String code;

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

  /** Corps du template (texte) */
  @Column(name = "corps_texte", columnDefinition = "TEXT")
  private String corpsTexte;

  /** Corps du template (HTML) */
  @Column(name = "corps_html", columnDefinition = "TEXT")
  private String corpsHtml;

  /** Variables disponibles (JSON) */
  @Column(name = "variables_disponibles", columnDefinition = "TEXT")
  private String variablesDisponibles;

  /** Canaux supportés (JSON array) */
  @Column(name = "canaux_supportes", length = 500)
  private String canauxSupportes;

  /** Langue du template */
  @Column(name = "langue", length = 10)
  private String langue;

  /** Description */
  @Column(name = "description", length = 1000)
  private String description;

  /** Notifications utilisant ce template */
  @JsonIgnore
  @OneToMany(mappedBy = "template", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @Builder.Default
  private List<Notification> notifications = new ArrayList<>();

  /** Callback JPA avant la persistance */
  @PrePersist
  protected void onCreate() {
    super.onCreate();
    if (langue == null || langue.isEmpty()) {
      langue = "fr";
    }
  }
}

