package dev.lions.unionflow.server.entity;

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

/**
 * Table de liaison entre Paiement et InscriptionEvenement
 *
 * @author UnionFlow Team
 * @version 3.0
 * @since 2025-01-29
 */
@Entity
@Table(
    name = "paiements_evenements",
    indexes = {
      @Index(name = "idx_paiement_evenement_paiement", columnList = "paiement_id"),
      @Index(name = "idx_paiement_evenement_inscription", columnList = "inscription_evenement_id")
    },
    uniqueConstraints = {
      @UniqueConstraint(
          name = "uk_paiement_evenement",
          columnNames = {"paiement_id", "inscription_evenement_id"})
    })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class PaiementEvenement extends BaseEntity {

  /** Paiement */
  @NotNull
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "paiement_id", nullable = false)
  private Paiement paiement;

  /** Inscription à l'événement */
  @NotNull
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "inscription_evenement_id", nullable = false)
  private InscriptionEvenement inscriptionEvenement;

  /** Montant appliqué à cette inscription */
  @NotNull
  @DecimalMin(value = "0.0", message = "Le montant appliqué doit être positif")
  @Digits(integer = 12, fraction = 2)
  @Column(name = "montant_applique", nullable = false, precision = 14, scale = 2)
  private BigDecimal montantApplique;

  /** Date d'application */
  @Column(name = "date_application")
  private LocalDateTime dateApplication;

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

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

