package gc.mda.kcg.domain.event;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.Map;

/**
 * AI 예측 알림.
 * 이벤트 발생 시 발송된 알림(SMS, 푸시 등) 이력을 저장.
 */
@Entity
@Table(name = "prediction_alerts", schema = "kcg")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @Builder
public class PredictionAlert {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "event_id")
    private Long eventId;

    @Column(name = "channel", length = 20)
    private String channel;

    @Column(name = "recipient", length = 200)
    private String recipient;

    @Column(name = "sent_at")
    private OffsetDateTime sentAt;

    @Column(name = "delivery_status", nullable = false, length = 20)
    private String deliveryStatus;

    @Column(name = "ai_confidence", precision = 5, scale = 4)
    private BigDecimal aiConfidence;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(columnDefinition = "jsonb")
    private Map<String, Object> metadata;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "event_id", insertable = false, updatable = false)
    private PredictionEvent event;

    @PrePersist
    void prePersist() {
        if (deliveryStatus == null) deliveryStatus = "SENT";
        if (sentAt == null) sentAt = OffsetDateTime.now();
    }
}
