package dev.lions.unionflow.server.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Index;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.DecimalMin;
import java.math.BigDecimal;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * Taux de change quotidien entre deux {@link Devise}s.
 *
 * <p>Source : BCEAO (officiel UEMOA), ECB, Fixer.io ou import manuel. Conservation
 * historique pour audit et conversions rétroactives.
 *
 * @since 2026-04-25 (P2-NEW-7)
 */
@Entity
@Table(name = "taux_change",
    uniqueConstraints = @UniqueConstraint(
        name = "uq_taux_change_paire_date",
        columnNames = {"devise_source", "devise_cible", "date_validite"}),
    indexes = {
        @Index(name = "idx_taux_change_paire", columnList = "devise_source,devise_cible"),
        @Index(name = "idx_taux_change_date_validite", columnList = "date_validite")
    })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class TauxChange extends BaseEntity {

  @Enumerated(EnumType.STRING)
  @Column(name = "devise_source", nullable = false, length = 3)
  private Devise deviseSource;

  @Enumerated(EnumType.STRING)
  @Column(name = "devise_cible", nullable = false, length = 3)
  private Devise deviseCible;

  /** 1 unité de {@code deviseSource} = {@code taux} unités de {@code deviseCible}. */
  @DecimalMin(value = "0.00000001", inclusive = false)
  @Column(name = "taux", nullable = false, precision = 18, scale = 8)
  private BigDecimal taux;

  @Column(name = "date_validite", nullable = false)
  private LocalDate dateValidite;

  @Builder.Default
  @Column(name = "source", nullable = false, length = 50)
  private String source = "BCEAO";
}
