package com.honey.honey.model;

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

import java.math.BigDecimal;
import java.time.Instant;

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

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

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

    @Column(name = "order_id", nullable = false, unique = true, length = 255)
    private String orderId;

    @Column(name = "stars_amount", nullable = false)
    private Integer starsAmount;

    @Column(name = "usd_amount", precision = 20, scale = 2)
    private BigDecimal usdAmount; // stored as decimal, e.g. 1.25 USD = 1.25

    @Column(name = "tickets_amount", nullable = false)
    private Long ticketsAmount; // Tickets amount in bigint format (legacy Stars)

    @Column(name = "play_balance")
    private Long playBalance; // Play balance in bigint (1 USD = 10_000_000_000); used for crypto deposits

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

    @Column(name = "telegram_payment_charge_id", length = 255)
    private String telegramPaymentChargeId;

    @Column(name = "telegram_provider_payment_charge_id", length = 255)
    private String telegramProviderPaymentChargeId;

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

    @Column(name = "completed_at")
    private Instant completedAt;

    /** True when this completed payment is the user's first deposit (crypto external completion). */
    @Column(name = "ftd", nullable = false)
    @Builder.Default
    private Boolean ftd = false;

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

    public enum PaymentStatus {
        PENDING, COMPLETED, FAILED, CANCELLED
    }
}




