package com.honey.honey.model;

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

import java.time.Instant;

@Entity
@Table(name = "promotions")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Promotion {

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

    @Enumerated(EnumType.STRING)
    @Column(name = "type", nullable = false, length = 32, columnDefinition = "VARCHAR(32)")
    private PromotionType type;

    @Column(name = "start_time", nullable = false)
    private Instant startTime;

    @Column(name = "end_time", nullable = false)
    private Instant endTime;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20, columnDefinition = "VARCHAR(20)")
    @Builder.Default
    private PromotionStatus status = PromotionStatus.PLANNED;

    /** Total prize fund in bigint (1 ticket = 1_000_000). */
    @Column(name = "total_reward")
    private Long totalReward;

    @Column(name = "created_at", nullable = false, updatable = false)
    private Instant createdAt;

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

    @PrePersist
    protected void onCreate() {
        Instant now = Instant.now();
        if (createdAt == null) createdAt = now;
        if (updatedAt == null) updatedAt = now;
    }

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

    public enum PromotionType {
        NET_WIN,
        /** Same as NET_WIN but points only when winner made max bet in the room. */
        NET_WIN_MAX_BET,
        /** 1 point per referral (level 1) who played at least one round (awarded when referral completes first round). */
        REF_COUNT
    }

    public enum PromotionStatus {
        ACTIVE,
        INACTIVE,
        FINISHED,
        PLANNED
    }
}
