package dev.lions.unionflow.server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * Don reçu (numéraire, nature, bénévolat, legs) — comptabilisé selon SYCEBNL :
 *
 * <ul>
 *   <li>NUMERAIRE → Crédit 755 (Dons et libéralités)
 *   <li>NATURE → valorisation obligatoire au prix marché, idem 755
 *   <li>BENEVOLAT → valorisation possible en notes annexes
 *   <li>LEGS → Crédit 756 ou poste dédié selon nature
 *   <li>FONDS_DEDIE → Crédit 19 (Fonds dédiés non utilisés, à reverser si finalité non remplie)
 * </ul>
 *
 * @since 2026-04-25 (P1-NEW-13)
 */
@Entity
@Table(name = "dons_recus", indexes = {
    @Index(name = "idx_don_org", columnList = "organisation_id"),
    @Index(name = "idx_don_donateur", columnList = "donateur_id"),
    @Index(name = "idx_don_date", columnList = "date_don")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class DonRecu extends BaseEntity {

  @NotNull
  @Column(name = "organisation_id", nullable = false)
  private UUID organisationId;

  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "donateur_id")
  private Donateur donateur;

  @NotBlank
  @Column(name = "type_don", nullable = false, length = 20)
  private String typeDon; // NUMERAIRE, NATURE, BENEVOLAT, LEGS

  @Column(name = "montant_xof", precision = 15, scale = 2)
  private BigDecimal montantXof;

  @Column(name = "valorisation_xof", precision = 15, scale = 2)
  private BigDecimal valorisationXof;

  @Column(name = "description", columnDefinition = "TEXT")
  private String description;

  @NotNull
  @Column(name = "date_don", nullable = false)
  private LocalDate dateDon;

  @NotBlank
  @Column(name = "affectation", nullable = false, length = 50)
  @Builder.Default
  private String affectation = "LIBRE"; // LIBRE, FONDS_DEDIE, PROJET_SPECIFIQUE

  @Column(name = "fonds_dedie_id")
  private UUID fondsDedieId;

  @Column(name = "projet_id")
  private UUID projetId;

  @Column(name = "recu_emis", nullable = false)
  @Builder.Default
  private boolean recuEmis = false;

  @Column(name = "numero_recu", length = 50)
  private String numeroRecu;

  @Column(name = "date_emission_recu")
  private LocalDate dateEmissionRecu;
}
