package dev.lions.unionflow.server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * Entité PieceJointe pour l'association flexible de documents
 *
 * @author UnionFlow Team
 * @version 3.0
 * @since 2025-01-29
 */
@Entity
@Table(
    name = "pieces_jointes",
    indexes = {
      @Index(name = "idx_piece_jointe_document", columnList = "document_id"),
      @Index(name = "idx_piece_jointe_membre", columnList = "membre_id"),
      @Index(name = "idx_piece_jointe_organisation", columnList = "organisation_id"),
      @Index(name = "idx_piece_jointe_cotisation", columnList = "cotisation_id"),
      @Index(name = "idx_piece_jointe_adhesion", columnList = "adhesion_id"),
      @Index(name = "idx_piece_jointe_demande_aide", columnList = "demande_aide_id"),
      @Index(name = "idx_piece_jointe_transaction_wave", columnList = "transaction_wave_id")
    })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class PieceJointe extends BaseEntity {

  /** Ordre d'affichage */
  @NotNull
  @Min(value = 1, message = "L'ordre doit être positif")
  @Column(name = "ordre", nullable = false)
  private Integer ordre;

  /** Libellé de la pièce jointe */
  @Column(name = "libelle", length = 200)
  private String libelle;

  /** Commentaire */
  @Column(name = "commentaire", length = 500)
  private String commentaire;

  /** Document associé */
  @NotNull
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "document_id", nullable = false)
  private Document document;

  // Relations flexibles (une seule doit être renseignée)
  @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 = "cotisation_id")
  private Cotisation cotisation;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "adhesion_id")
  private Adhesion adhesion;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "demande_aide_id")
  private DemandeAide demandeAide;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "transaction_wave_id")
  private TransactionWave transactionWave;

  /** Méthode métier pour vérifier qu'une seule relation est renseignée */
  public boolean isValide() {
    int count = 0;
    if (membre != null) count++;
    if (organisation != null) count++;
    if (cotisation != null) count++;
    if (adhesion != null) count++;
    if (demandeAide != null) count++;
    if (transactionWave != null) count++;
    return count == 1; // Exactement une relation doit être renseignée
  }

  /** Callback JPA avant la persistance */
  @PrePersist
  protected void onCreate() {
    super.onCreate();
    if (ordre == null) {
      ordre = 1;
    }
  }
}

