package com.honey.honey.model;

import jakarta.persistence.*;
import lombok.*;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.Objects;

@Entity
@Table(name = "promotions_users")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@IdClass(PromotionUser.PromotionUserId.class)
public class PromotionUser {

    @Id
    @Column(name = "promo_id", nullable = false)
    private Integer promoId;

    @Id
    @Column(name = "user_id", nullable = false)
    private Integer userId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "promo_id", nullable = false, insertable = false, updatable = false)
    private Promotion promotion;

    @Column(name = "points", nullable = false, precision = 20, scale = 2)
    @Builder.Default
    private BigDecimal points = BigDecimal.ZERO;

    @Column(name = "updated_at", nullable = false)
    private Instant updatedAt;

    @PrePersist
    @PreUpdate
    protected void onUpdate() {
        updatedAt = Instant.now();
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PromotionUserId implements Serializable {
        private Integer promoId;
        private Integer userId;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            PromotionUserId that = (PromotionUserId) o;
            return Objects.equals(promoId, that.promoId) && Objects.equals(userId, that.userId);
        }

        @Override
        public int hashCode() {
            return Objects.hash(promoId, userId);
        }
    }
}
