package com.honey.honey.model;

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

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

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

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

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

    @Column(name = "username", nullable = false, length = 255)
    private String username;

    @Column(name = "wallet", length = 120)
    private String wallet;

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

    @Enumerated(EnumType.STRING)
    @Column(name = "gift_name", length = 50, columnDefinition = "VARCHAR(50)")
    private GiftType giftName;

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

    @Column(name = "total", nullable = false)
    private Long total; // Tickets amount in bigint format

    @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 = "amount_coins", length = 50)
    private String amountCoins;

    @Column(name = "commission_coins", length = 50)
    private String commissionCoins;

    @Column(name = "amount_to_send", length = 50)
    private String amountToSend;

    @Column(name = "payment_id")
    private Integer paymentId; // Crypto API payment id from withdrawal response

    @Column(name = "txhash", length = 255)
    private String txhash; // Transaction hash from crypto API (WithdrawalInfoApiResponse.PaymentItem)

    @Column(name = "quantity", nullable = false)
    @Builder.Default
    private Integer quantity = 1;

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

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

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

    @Column(name = "resolved_at")
    private Instant resolvedAt;

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

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

    public enum PayoutType {
        STARS, GIFT, CRYPTO
    }

    public enum PayoutStatus {
        PROCESSING, COMPLETED, CANCELLED, WAITING
    }

    public enum GiftType {
        HEART, BEAR, GIFTBOX, FLOWER, CAKE, BOUQUET, ROCKET, CUP, RING, DIAMOND, CHAMPAGNE
    }
}

